EOF on input file

Posted on 1998-09-04
Last Modified: 2010-04-30
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

    Close #1
End Sub
Question by:jalymo
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 1433112
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.

Expert Comment

ID: 1433113
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

Accepted Solution

SPECIALIST earned 20 total points
ID: 1433114
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.


Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 1433115
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.

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:

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

Author Comment

ID: 1433116
My test file contains 3 records, all containing the six values and it bombs with the EOF error.  The file is as follows:



Expert Comment

ID: 1433117
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?

Author Comment

ID: 1433118
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.

Expert Comment

ID: 1433119
"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.  :)

Author Comment

ID: 1433120
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.

Expert Comment

ID: 1433121
I figured as much.  :)
This is why I said not to worry about it.  I'm sure you'll get the hang of it.

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Modifying Conditional Format from VBA code 3 71
Prevent user closing word document opened with VB6 6 84
Automatic Email Reminder 4 74
Dinamic report to Crosstab query 9 52
Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question