8

I need to read data from text files and use the same in my application. Im using VB 6.0. What commands do I use? Some Sample code would be highly appreciated.

eglease
  • 2,445
  • 11
  • 18
  • 28
Jack Njiri
  • 346
  • 1
  • 5
  • 12

7 Answers7

19

Here's how to read an entire text file into a string - from the VB6 manual.

Function FileToString(strFilename As String) As String
  iFile = FreeFile
  Open strFilename For Input As #iFile
    FileToString = StrConv(InputB(LOF(iFile), iFile), vbUnicode)
  Close #iFile
End Function
MarkJ
  • 30,070
  • 5
  • 68
  • 111
  • I'm reading a decompressed pdf file and getting error "Input past end of file" on the InputB line – Shodan Oct 09 '16 at 18:08
  • I converted it into a function as suggested by @Shodan – MarkJ Oct 10 '16 at 12:10
  • @Shodan that "Input past end of file" error is odd. What is LOF returning? It [should be](https://msdn.microsoft.com/en-us/library/aa445067(v=vs.60).aspx) the length of the file in bytes. And then InputB is [expecting to be passed](https://msdn.microsoft.com/en-us/library/aa445027(v=vs.60).aspx) the number of bytes to read, so it's hard to see how it can read past the end of the file. – MarkJ Oct 10 '16 at 12:15
14

A full tutorial and sample code can be found here

  Open Filename$ For Input As #FileHandle

  Do While Not EOF(FileHandle)        ' Loop until end of file
   Line Input #FileHandle, TextLine$  ' Read line into variable
    ' Your code here
  Loop

  Close #FileHandle
David Sykes
  • 48,469
  • 17
  • 71
  • 80
3

I'm a little late to the game here, but the FileSystemObject that is part of the Microsoft Scripting Runtime (scrrun.dll) can be pretty useful for this.

Public Function ReadTextFileAsString(IN_sFilePath As String) As String
    Dim myFSO As Scripting.FileSystemObject
    Dim myTextStream As Scripting.TextStream
    Dim myString As String

    'Create a new FileSystemObject
    Set myFSO = New Scripting.FileSystemObject

    'Make sure file exists:
    If myFSO.FileExists(IN_sFilePath) Then
        Set myTextStream = myFSO.OpenTextFile(IN_sFilePath, ForReading)
        myString = myTextStream.ReadAll()
        Call myTextStream.Close
    End If
    'Assign Return Value
    ReadTextFileAsString = myString

    'Make sure to clean up when done.
    Set myTextStream = Nothing
    Set myFSO = Nothing
End Function

There are a number of other methods available for getting data from the text stream. You can also read a certain number of characters at a time, or line-by-line. You will need to add the Microsoft Scripting Runtime into your project references, but it is really very useful.

nedmech
  • 58
  • 1
  • 6
1

Make sure your file exists:

If myFSO.FileExists(IN_sFilePath) Then
    Set myTextStream = myFSO.OpenTextFile(IN_sFilePath, ForReading)
    myString = myTextStream.ReadAll()
    Call myTextStream.Close
End If
'Assign Return Value
ReadTextFileAsString = myString
senshin
  • 10,022
  • 7
  • 46
  • 59
Sarpsar
  • 11
  • 1
1

if there is just plain text in the file then you can read in the whole into 1 string variable with the following code :

Private Sub ReadFile(strFile As String)
  Dim intFile As Integer
  Dim strData As String
  intFile = FreeFile
  Open strFile For Input As #intFile
    strData = Input(LOF(intFile), #intFile)
  Close #intFile
End Sub

a variable-length string can contain up to approximately 2 billion (2^31) characters

Hrqls
  • 2,944
  • 4
  • 34
  • 54
0

Here is the code for that

Function ReadFileToText(filePath)

   Dim objFile, objText, text

   Set objFile = CreateObject("Scripting.FileSystemObject")
   Set objText = objFile.OpenTextFile(filePath)

   text = objText.ReadAll
   objText.Close

   Set objText = Nothing
   Set objFile = Nothing

   ReadFileToText = text

End Function
Palanikumar
  • 6,940
  • 4
  • 40
  • 51
-1

i will refer you a different method to read and import the content to your form window

public sub readfile
    Dim rtc As TextBox = New TextBox
    rtc.Multiline = True
    rtc.ScrollBars = ScrollBars.Both
    rtc.Width = 400
    rtc.Height = 200
    Me.Controls.Add(rtc)
    rtc.WordWrap = True
    Dim FILE_NAME As String = "C:\Users\vcidex92\Desktop\suji\me.html"

    If System.IO.File.Exists(FILE_NAME) = True Then

        Dim objReader As New System.IO.StreamReader(FILE_NAME)
        rtc.Text = objReader.ReadToEnd
        objReader.Close()
    Else

        MsgBox("File Does Not Exist")
    End If
end sub
TAbdiukov
  • 1,185
  • 3
  • 12
  • 25