1 Attachment(s)
VB5 - Open Text File In Text Box
I've looked for and tried many different methods of trying to open a text file and show the insides inside a text box, but i can't find one that works.
I'll attach the file i'm trying to do it with, basically when you add a name to the list it also creates a text file and i want that new textfile to load inside the bottom text area when you click the name in the list. You'll understand when you've seen the form. :)
Re: VB5 - Open Text File In Text Box
Code:
Input #3, Text2.Text
That is not allowed, but this works:
Code:
Dim s As String
Input #3, s: Text2.Text = s
Keep in mind though, that Input will read until the end of line and not further. This will read the whole file:
Code:
Open App.Path & List1.Text & ".txt" For Binary Access Read Shared As #3
Dim s As String
s= Space(LOF(3))
Get #3, 1, s: Text2.Text = s
Close #3
Also, I can advise you to use FreeFile instead of static file handles like this:
Code:
Dim ff As Integer
ff = FreeFile
Open App.Path & List1.Text & ".txt" For Binary Access Read Shared As #ff
Re: VB5 - Open Text File In Text Box
Thanks, that worked. How would i now go about saving this text box to the textfile with the press of a button. I've tried doing it like this...
vb Code:
Private Sub Command2_Click()
Open App.Path & List1.Text & ".txt" For Input As #4
Print #4, Text2.Text
Close #4
End Sub
But it doesn't work, any ideas?
Re: VB5 - Open Text File In Text Box
You use "For Input" while reading, so you should use "For Output" while writing.
Re: VB5 - Open Text File In Text Box
Great :D Thanks, just tried the saving with using multiline but after i've saved it and then try and view it in the text box again it doesnt show anything below the first line? However the Text file has the multiple line... Ideas?
Re: VB5 - Open Text File In Text Box
Could you post the whole code again please? Then I will take a look. I want to see your last changes :)
Re: VB5 - Open Text File In Text Box
vb Code:
Option Explicit
Private Sub Command1_Click()
List1.AddItem Text1.Text
Text1.SetFocus
Dim i As Integer
If List1.ListCount = 0 Then Exit Sub
Open App.Path & "clients.txt" For Output As #1
For i = 0 To List1.ListCount - 1
Print #1, List1.List(i)
Next i
Close #1
Open App.Path & Text1.Text & ".txt" For Append As #2
Print #2, "Names: " & Text1.Text
Close #2
End Sub
Private Sub List1_Click()
Open App.Path & List1.Text & ".txt" For Input As #3
Dim s As String
Input #3, s: Text2.Text = s
Close #3
End Sub
Private Sub Command2_Click()
Open App.Path & List1.Text & ".txt" For Output As #4
Print #4, Text2.Text
Close #4
End Sub
'populate list when form loads
Private Sub Form_Load()
Dim strLine As String
If Not Dir(App.Path & "clients.txt") = "" Then
Open App.Path & "clients.txt" For Input As #1
Do While Not EOF(1)
Line Input #1, strLine
List1.AddItem strLine
Loop
Close #1
End If
End Sub
Re: VB5 - Open Text File In Text Box
As I suggested earlier, change
Code:
Private Sub List1_Click()
Open App.Path & List1.Text & ".txt" For Input As #3
Dim s As String
Input #3, s: Text2.Text = s
Close #3
End Sub
Into
Code:
Private Sub List1_Click()
Open App.Path & List1.Text & ".txt" For Binary Access Read Shared As #3
Dim s As String
s= Space(LOF(3))
Get #3, 1, s: Text2.Text = s
Close #3
End Sub
Then you will read the whole file into Text2.Text
Re: VB5 - Open Text File In Text Box
Once again, big thanks.. I think that's everything, just trying to make it delete the text file with a delete button now. Tried
Code:
Kill App.Path & List1.Text & ".txt"
but it doesn't seem to work. Other than that, that's everything i'm gonna need for a long time hopefully. :D
Re: VB5 - Open Text File In Text Box
Do you get an error while trying to kill the file?
Re: VB5 - Open Text File In Text Box
Erm not now, but it still doesn't delete the file..
vb Code:
Private Sub Command3_Click()
If MsgBox("Are You Sure?", vbYesNo, "Title") = vbYes Then
List1.RemoveItem (List1.ListIndex)
Dim i As Integer
If List1.ListCount = 0 Then Exit Sub
Open App.Path & "clients.txt" For Output As #1
For i = 0 To List1.ListCount - 1
Print #1, List1.List(i)
Next i
Close #1
On Error Resume Next
Kill App.Path & List1.Text & ".txt"
End If
End Sub
Is the whole button code, it's removing the statement from the list, but it wont delete the file?
Re: VB5 - Open Text File In Text Box
If you remove "On Error Resume Next" in that piece of code, it will probably give an error.
Re: VB5 - Open Text File In Text Box
Re: VB5 - Open Text File In Text Box
Ah now I read your code more specific I see what went wrong:
First you do: List1.RemoveItem
Then you do: Kill App.Path & List1.Text & ".txt"
But at the second command, list1.text is no more, you removed that item earlier.
If you delete the actual file before removing it from the list the problem should be solved.
Good luck :)
Re: VB5 - Open Text File In Text Box
Oh of course! How thick can i get :lol: thats very simple, thanks a bunch for pointing that out.. Would've taken me hours to realise that was my problem. Big Help :)