|
-
Oct 13th, 2004, 03:18 AM
#1
Thread Starter
Member
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:
Private Sub cmdShowOpen_Click()
On Error GoTo errHandler
With CommonDialog1
.Filter = "Text (*.txt)|*.txt"
.InitDir = "C:\Program Files\My Files"
.ShowOpen
If Len(.FileName) Then
Open .FileName For Input As #1
txtLog.Text = Input$(LOF(1), #1)
Close #1
End If
End With
Exit Sub
errHandler:
MsgBox Err.Number & " " & Err.Description
End Sub
Last edited by iori85z; Oct 13th, 2004 at 03:51 AM.
-
Oct 13th, 2004, 03:41 AM
#2
Retired VBF Adm1nistrator
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:
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 [b]Binary[/b]
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
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
-
Oct 13th, 2004, 03:51 AM
#3
Thread Starter
Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|