Subject: Text File
 
Question:
Is there a way to read data or strings from a .txt file and if so how?

Thank you
 
Answer:
The most common way to read a text file, is the Open method. To read from a file, will use the Input keyword:
Open FileName for Input as #1
For example:
Open “C:\MyText.txt”
Than, to read a line, use the Line Input method:
Line Input #FileNumber, StringVarable
For example:
Line Input #1, FirstLine
You can use the EOF (End Of File) function to determine when you are on the last line:
EOF(FileNumber)
For example: If EOF(1) then MsgBox (“Compleate”)
The last method that you need to know is the Close method:
Close #FileNumber This method clothes the file.
 
This is a simple code for a program that prints a file:
 
Private Sub Form_Load()
Form1.Show 'loads the form.
Open "C:\mytext.txt"
For Input As #1
Dim LastLine As String
Dim AllText As String
Do Until EOF(1)
Line Input #1, LastLine
AllText = AllText & LastLine & vbNewLine
Loop
Print AllText
End Sub
 
Home Page  - Categories - Projects - TextFiles
Copyright 2002 All rights reserved to vb4all.8m.net