Read A Text File using VB6 / Classic


The following example shows how you can open a textfile ( .txt) and read the data by using Visual Basic. Assuming the text file "contact.txt" stored in C:\ drive contains the following 3 lines (Name, Contact Number and Address)


----------------------contact.txt---------------------

Kimberly Wood
570-897-7676
227 Main Avenue PO Box 987, Palmerton, PA 18071

------------------------------------------------------


Now, to read the following three lines of data in visual basic, use the example shown below


'-----declare variables

Dim ReadData As String
Dim FileNum As Integer

Dim Name As String
Dim ContactNo As String
Dim Address As String


FileNum = FreeFile

On Error GoTo ErrorHandler

Open "C:\contact.txt" For Input As #filenum '---Open the file for reading

Input #filenum, ReadData '---read line 1
Name = ReadData '-----store data into variable Name
Input #filenum, ReadData '---read line 2
ContactNo = ReadData '-----store data into variable ContactNo
Input #filenum, ReadData '---read line 3
Address = ReadData '-----store data into variable Address

ErrorHandler:
Close #filenum '---Close the file after reading


Finally, the three line of data from the text file is now read and stored into the three variables Name, ContactNo and Address in Visual Basic.

See also how to Write Data Into A Text File Using VB6 / Classic
See also how to Read Data From A Text File Using VB.NET
See also how to Write Data Into A Text File Using VB.NET

0 comments