Link to home
Start Free TrialLog in
Avatar of alangsk
alangsk

asked on

Run Time Error '62': Input Past End Of File

I am saving and reading data from a text file.  If I create the file first with Notepad, it works fine, but when I execute btnaddfav_Click and I try to read the text file again, I get error 62.  Is there something I am doing wrong?  Please help.

This is what the text file looks like:
"Carputer","www.mp3car.com",0,0
"XBOX 2 News","http://xbox2news.com/",0,0
"CNN","http://www.cnn.com/",0,0
"XBOX Scene","http://xbox-scene.com/",0,0
"CNN Test","http://www.cnn.com/",0,0

Here is the code I am using:
This adds the currunt info to the text file.
Private Sub btnaddfav_Click()
    favscrx = scrhpos
    favscry = scrvpos
    favurl = brw.LocationURL
    nicname = InputBox("Enter Name For Favorite")
    filename = App.Path & "\fav.dat"
    ifile = FreeFile
    Open filename For Append As #ifile
        Write #ifile, nicname, favurl, favscrx, favscry
    Close #ifile
    favlst.AddItem nicname
End Sub

This reads the info from the text file and populates a list box.
Private Sub btnfav_Click()
    filename = App.Path & "\fav.dat"
    ifile = FreeFile
    If favlst.Visible = True Then
        favlst.Visible = False
    Else
        favlst.Clear
        Open filename For Input As #ifile
        Do While Not EOF(ifile)
            Input #ifile, nicname, favurl, favscrx, favscry
            favlst.AddItem nicname
        Loop
        Close #ifile
        favlst.Visible = True
    End If
End Sub

This reads the info from the text file and saves it to a new file minus the line that was selected in the listbox.
Private Sub btnremfav_Click()
    Dim i As Integer
    Dim bfilename As String
    bfilename = App.Path & "\temp.dat"
    selname = favlst.Text
    filename = App.Path & "\fav.dat"
    'Remove the line from the list box
    For i = 0 To favlst.ListCount - 1
        If favlst.List(i) = selname Then
            favlst.RemoveItem i
            Exit For
        End If
    Next i
    'Create a new file with the selected line removed
    Open filename For Input As #1
    Open bfilename For Output As #2
        Do Until (EOF(1))
Start:
            Input #1, nicname, favurl, favscrx, favscry
            If nicname = selname Then
                GoTo Start
            Else
                Write #2, nicname, favurl, favscrx, favscry
            End If
        Loop
    Close #1
    Close #2
    'Delete original file and rename new file as original
    Kill filename
    Name bfilename As filename
End Sub
Avatar of Rubyn
Rubyn

Your Code is working fine....
Tell me place where exactly you are getting the error.
ASKER CERTIFIED SOLUTION
Avatar of Rubyn
Rubyn

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of alangsk

ASKER

Thanks Rubyn!  That fixed the problem.