Hi for some reason when i do this, i put text1.text = Text1.Text = sFileText
and it only loads the last line ?
Any ideas (I'm using the first code example you gave!)
Printable View
Hi for some reason when i do this, i put text1.text = Text1.Text = sFileText
and it only loads the last line ?
Any ideas (I'm using the first code example you gave!)
Post moved from the FAQ forum, which is not the place to post your questions.
This is the thread it was moved from: http://www.vbforums.com/showthread.p...te-a-text-file
vb Code:
Do While Not EOF(iFileNo) Input #iFileNo, sFileText 'show the text (you will probably want to replace this line as appropriate to your program!) Text1.Text = Text1.Text & sFileText Loop
You got "=" and "&" muddled up.
I would suggest a little different approach. Using input # in such a way would be rather slow and depending on the file may not give you the result you are looking for.
For example if the file you are reading is a CSV file Input # will strip out all the commas, if the file is multiline then it will also strip out of the CRLFs.
You can use a variant of the Input# method to read the entire file at once and place the whole file in the text box which is much faster and does not strip out any characters.
You can also use Line Input# method to read each line which wil also strip out CRLFs but it you can easily add them as you go because you will always be able to tell where they should be. This is not the case with the plain Input# method since it breaks up the data on commas and CRLFs there is no way to properly reconstruct a CSV file in a text box unless you know the structure and add all the commas and CRLFs back as you read it.
what DataMiser wrote is true... however i like to load line by line and is still really fast here is an approach like nightwalker wrote but he forget two details :) (in red)
here is something you can tryCode:Open "C:\TextFile.txt" For Input As #1
Do Until EOF(1)
Line Input #1, TextByLine
Text1.Text = Text1.Text & TextByLine & vbNewLine
Loop
Text1.Text = Left(Text1, Len(Text1) - 1) 'Remove Last vbNewLine
Close #1
you can remove my If Text1.Multiline = False... End if it is not necessary
Code:Private Sub Command1_Click()
Dim TextByLine As String
If Text1.MultiLine = False Then
MsgBox "Please make Text1 Multiline 'True' at design time!", vbCritical, "Error"
Exit Sub
End If
Text1.Text = ""
Open "C:\TextFile.txt" For Input As #1
Do Until EOF(1)
Line Input #1, TextByLine
Text1.Text = Text1.Text & TextByLine & vbNewLine
Loop
Text1.Text = Left(Text1, Len(Text1) - 1) 'Remove Last vbNewLine
Close #1
End Sub