How to read data from a text file using VB6?

I need to read data from text files and use them in my application. Im using VB 6.0. What commands do I use? Some code examples will be greatly appreciated.

+6
vb6
source share
7 answers

Here's how to read the entire text file in a line - 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 
+15
source share

A complete 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 
+11
source share

I'm a little late to the game here, but FileSystemObject, which is part of Microsoft Scripting Runtime (scrrun.dll), can be very 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 many other methods for retrieving data from a text stream. You can also read a certain number of characters at a time or in turn. You will need to add Microsoft Scripting Runtime Runtime to your project links, but it is really very useful.

+3
source share

Make sure the 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 
+1
source share

if there is only plain text in the file, you can read a whole single 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 2 billion characters (2 ^ 31)

0
source share

Here is the code for this

 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 

You can check out more detailed information at http://smartreferences.blogspot.in

0
source share

i will refer to another method of reading and importing content into the 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 
-one
source share

All Articles