Results 1 to 3 of 3

Thread: Error Opening Text Files [Resolved]

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2004
    Posts
    60

    Resolved Error Opening Text Files [Resolved]

    Hi, I have a GUI with a button and a textbox. And when I press open, it should open up the text file which I chose. It works fine until I tried to open up a text file with a , on the last line of the text. It will generate an error 62, past end of file. Is there any modifications I can do to my code to solve this problem because most of the textfiles I open have a , at the last line, therefore, if this problem is not solve, my GUI will not serve its purpose. Thanks.

    The code for the button is:

    VB Code:
    1. Private Sub cmdShowOpen_Click()
    2.  
    3. On Error GoTo errHandler
    4.  
    5.     With CommonDialog1
    6.         .Filter = "Text (*.txt)|*.txt"
    7.         .InitDir = "C:\Program Files\My Files"
    8.         .ShowOpen
    9.        
    10.     If Len(.FileName) Then
    11.     Open .FileName For Input As #1
    12.     txtLog.Text = Input$(LOF(1), #1)
    13.     Close #1
    14.     End If
    15.    
    16.     End With
    17.  
    18. Exit Sub
    19. errHandler:
    20.     MsgBox Err.Number & " " & Err.Description
    21.    
    22. End Sub
    Last edited by iori85z; Oct 13th, 2004 at 03:51 AM.

  2. #2
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    The problem is that you're opening your file For Input, which assumes a text-file - and not a binary file. To open a file for binary (which I suggest you do anyway irresepctive of the file type), use the following code:

    VB Code:
    1. Private Sub cmdShowOpen_Click()
    2.  
    3. On Error GoTo errHandler
    4.     Dim strBuff As String
    5.  
    6.     With CommonDialog1
    7.         .Filter = "Text (*.txt)|*.txt"
    8.         .InitDir = "C:\Program Files\My Files"
    9.         .ShowOpen
    10.         If Len(.FileName) Then
    11.             Open .FileName For [b]Binary[/b]
    12.                 strBuff = Space(Lof(1))
    13.                 Get #1, , strBuff
    14.                 txtLog.Text = strBuff
    15.                 strBuff = vbNullString
    16.             Close #1
    17.         End If
    18.     End With
    19.  
    20. Exit Sub
    21.  
    22. errHandler:
    23.     MsgBox Err.Number & " " & Err.Description    
    24. End Sub
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  3. #3

    Thread Starter
    Member
    Join Date
    Sep 2004
    Posts
    60
    Oh ok, I changed my codes to the one below after looking at urs and it works, thanks.


    Private Sub cmdShowOpen_Click()

    On Error GoTo errHandler
    Dim strBuff As String

    With CommonDialog1
    .Filter = "Text (*.txt)|*.txt"
    .InitDir = "C:\Program Files\My Files"
    .ShowOpen
    If Len(.FileName) Then
    Open .FileName For Binary As #1
    strBuff = Space(LOF(1))
    Get #1, , strBuff
    txtLog.Text = strBuff
    strBuff = vbNullString
    Close #1
    End If
    End With

    Exit Sub

    errHandler:
    MsgBox Err.Number & " " & Err.Description
    End Sub

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