Results 1 to 15 of 15

Thread: VB5 - Open Text File In Text Box

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2009
    Posts
    30

    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.
    Attached Files Attached Files

  2. #2
    Lively Member Garrcomm's Avatar
    Join Date
    Jul 2009
    Location
    the Netherlands
    Posts
    87

    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
    Last edited by Garrcomm; Jul 11th, 2009 at 03:58 PM. Reason: added some more details

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jul 2009
    Posts
    30

    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:
    1. Private Sub Command2_Click()
    2.  
    3.         Open App.Path & List1.Text & ".txt" For Input As #4
    4.             Print #4, Text2.Text
    5.         Close #4
    6.  
    7. End Sub

    But it doesn't work, any ideas?

  4. #4
    Lively Member Garrcomm's Avatar
    Join Date
    Jul 2009
    Location
    the Netherlands
    Posts
    87

    Re: VB5 - Open Text File In Text Box

    You use "For Input" while reading, so you should use "For Output" while writing.
    Last edited by Garrcomm; Jul 11th, 2009 at 04:32 PM.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jul 2009
    Posts
    30

    Re: VB5 - Open Text File In Text Box

    Great 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?

  6. #6
    Lively Member Garrcomm's Avatar
    Join Date
    Jul 2009
    Location
    the Netherlands
    Posts
    87

    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

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Jul 2009
    Posts
    30

    Re: VB5 - Open Text File In Text Box

    vb Code:
    1. Option Explicit
    2.  
    3. Private Sub Command1_Click()
    4.     List1.AddItem Text1.Text
    5.     Text1.SetFocus
    6.    
    7.        
    8.     Dim i As Integer
    9.  
    10.     If List1.ListCount = 0 Then Exit Sub
    11.    
    12.     Open App.Path & "clients.txt" For Output As #1
    13.         For i = 0 To List1.ListCount - 1
    14.             Print #1, List1.List(i)
    15.         Next i
    16.     Close #1
    17.    
    18.     Open App.Path & Text1.Text & ".txt" For Append As #2
    19.     Print #2, "Names: " & Text1.Text
    20.     Close #2
    21.    
    22. End Sub
    23.  
    24. Private Sub List1_Click()
    25.  
    26.         Open App.Path & List1.Text & ".txt" For Input As #3
    27.          Dim s As String
    28.         Input #3, s: Text2.Text = s
    29.         Close #3
    30.        
    31. End Sub
    32.  
    33. Private Sub Command2_Click()
    34.  
    35.         Open App.Path & List1.Text & ".txt" For Output As #4
    36.             Print #4, Text2.Text
    37.         Close #4
    38.  
    39. End Sub
    40.        
    41. 'populate list when form loads
    42. Private Sub Form_Load()
    43. Dim strLine As String
    44.  
    45.     If Not Dir(App.Path & "clients.txt") = "" Then
    46.         Open App.Path & "clients.txt" For Input As #1
    47.             Do While Not EOF(1)
    48.                 Line Input #1, strLine
    49.                 List1.AddItem strLine
    50.             Loop
    51.         Close #1
    52.     End If
    53.  
    54. End Sub

  8. #8
    Lively Member Garrcomm's Avatar
    Join Date
    Jul 2009
    Location
    the Netherlands
    Posts
    87

    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
    If a thread is solved, please click on Thread Tools / Mark Thread Resolved .
    If someone helped you very good, consider rating his post by clicking the icon under his name.

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Jul 2009
    Posts
    30

    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.

  10. #10
    Lively Member Garrcomm's Avatar
    Join Date
    Jul 2009
    Location
    the Netherlands
    Posts
    87

    Re: VB5 - Open Text File In Text Box

    Do you get an error while trying to kill the file?

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Jul 2009
    Posts
    30

    Re: VB5 - Open Text File In Text Box

    Erm not now, but it still doesn't delete the file..

    vb Code:
    1. Private Sub Command3_Click()
    2.     If MsgBox("Are You Sure?", vbYesNo, "Title") = vbYes Then
    3.  
    4.     List1.RemoveItem (List1.ListIndex)
    5.    
    6.     Dim i As Integer
    7.  
    8.     If List1.ListCount = 0 Then Exit Sub
    9.    
    10.     Open App.Path & "clients.txt" For Output As #1
    11.         For i = 0 To List1.ListCount - 1
    12.             Print #1, List1.List(i)
    13.         Next i
    14.     Close #1
    15.    
    16.    
    17.     On Error Resume Next
    18.         Kill App.Path & List1.Text & ".txt"
    19.  
    20.     End If
    21. End Sub

    Is the whole button code, it's removing the statement from the list, but it wont delete the file?

  12. #12
    Lively Member Garrcomm's Avatar
    Join Date
    Jul 2009
    Location
    the Netherlands
    Posts
    87

    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.

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Jul 2009
    Posts
    30

    Re: VB5 - Open Text File In Text Box


  14. #14
    Lively Member Garrcomm's Avatar
    Join Date
    Jul 2009
    Location
    the Netherlands
    Posts
    87

    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
    If a thread is solved, please click on Thread Tools / Mark Thread Resolved .
    If someone helped you very good, consider rating his post by clicking the icon under his name.

  15. #15

    Thread Starter
    Junior Member
    Join Date
    Jul 2009
    Posts
    30

    Re: VB5 - Open Text File In Text Box

    Oh of course! How thick can i get thats very simple, thanks a bunch for pointing that out.. Would've taken me hours to realise that was my problem. Big Help

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width