EOF on input file

Reading a comma delimited file and "Do While Not EOF" is not working as I would expect.  The following pgm gets an error 62 "input past end of file".  In order to get this to work, I had to put some dummy records at the end of the input file, test for the dummy values, then exit.


 Private Sub Command1_Click()
Dim UID         As String
Dim LASTN       As String
Dim FIRSTN      As String
Dim MI          As String
Dim MAIL        As String
Dim Phone       As String

Open "c:\mydocs\mystuff\user" For Input As #1

Do While Not EOF(1)
    Input #1, UID, LASTN, FIRSTN, MI, MAIL, Phone
    Debug.Print UID, LASTN, FIRSTN, MI, MAIL, Phone
    ' process record work
Loop

exit_close:
    Close #1
   
End Sub
jalymoAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
SPECIALISTConnect With a Mentor Commented:
I'm a little unclear, but you can add  On error resume next  
to the beginning of the file.  This will get rid of your error message.  just make sure the program is working the way you want and you are in business.


specialist

0
 
clifABBCommented:
Are you sure you have a mulitple of six entries in you file?

Because you are inputing six entries at a time, if you get to a point where there is only five left (ie someone didn't enter a phone number), you will get an error.
0
 
hozempaCommented:
jalymo,
ClifABB touches on an interesting point regarding the Line# Statement.  If all of the records(lines) in the source file do not contain exactly six variables separated by a comma, then Line# statement will produce errors (ie, begin reading the next line if the previous did not contain six variable or try to read past the EOF).  You may want to use the Line Input statement to read each line in at a time and then parse the line which is returned as a string using InStr and Mid.  It's a little more work but is usually worth it unless you can guarantee that each line will contain six entries.

good luck, hozempa
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
clifABBCommented:
SPECIALIST:
I disagree with your "answer".  This amounts to sweeping the problem under the rug.  You can always hide an error by using an "On Error Resume Next" but this doesn't fix the error.

jalymo:
I would suggest the following:
Make sure your file has records in multiples of six as this is most likely the problem.
To verify, break up your test input statement and watch the values:

Do
  If EOF(1) Then Exit Do
  Input #1, UID
  Debug.Print "UID = "; UID
  If EOF(1) Then Exit Do
  Input #1, LASTN
  Debug.Print "LASTN = ";LASTN
  If EOF(1) Then Exit Do
  Input #1, FIRSTN
  Debug.Print "FIRSTN = "; FIRSTN
  If EOF(1) Then Exit Do
  Input #1, MI
  Debug.Print "MI = "; MI
  If EOF(1) Then Exit Do
  Input #1, MAIL
  Debug.Print "MAIL = "; MAIL
  If EOF(1) Then Exit Do
  Input #1, Phone
  Debug.Print "Phone = "; Phone
Loop
Running the above test will let you know if you are missing data and will indicate where to look for it.

I can see the problem probably arises when you are writing data to the file where if, say, the MI is missing you skip writing to the file.  If there is any data missing, you must still at least write an empty string.
0
 
jalymoAuthor Commented:
My test file contains 3 records, all containing the six values and it bombs with the EOF error.  The file is as follows:

LJ1,JONES,LATOYA,R,EI-20,2025877324
WHG,HOLMES,WAYNE,L,EI-53,2024351291
TWW,WILLIAMS,TIMOTHY,H,EI-42,2025874631

 
0
 
clifABBCommented:
I have some bad news for you.

Using your original code (cut & paste from above), and using your test file, I end up with no errors.

Did you cut & paste the contents of your test file?  Or did you just re type it in here?
0
 
jalymoAuthor Commented:
I cut and pasted into a new file and it also worked for me!!  My original file was transferred from our mainframe and I now believe that the end of file marker was removed/lost/deleted.

Given the above responses, I vote to give the points to clifABB.

Thanks to all.
0
 
clifABBCommented:
"I vote to give the points to clifABB"
I appreciate the gesture, unfortunately you gave the points to specialist.  This occasionally happens, so don't worry about it.

Thanks anyway.  :)
0
 
jalymoAuthor Commented:
clifABB: I am new to Experts Exchange and now realize I should examine the options more closely to get the points to the right person.
0
 
clifABBCommented:
I figured as much.  :)
This is why I said not to worry about it.  I'm sure you'll get the hang of it.
0
All Courses

From novice to tech pro — start learning today.