Link to home
Start Free TrialLog in
Avatar of kkapush
kkapush

asked on

Input past end of file

I am trying to read a .txt file and as I read it in line-by-line, pase the data and append it to the appropriate table either Guests Names, Show Information and Orders.  I keep receiving the error...

 Runtime Error '62'.
"Input Past End of File."

I tried using the EOF and Lof functions as Microsoft suggests and neither work.  Can anyone help.  

Here is my code...

Open MyLoc For Input As #1    ' Open file for input.
Do While MyLocation < LOF(1)  ' Loop until end of file.
    MyLocation = Loc(1)
    Input #1, ModeNum, CCName, CCType, CCNo, CCEXP, SUDef1
    Debug.Print ModeNum, CCName, CCType, CCNo, CCEXP, SUDef1    ' Print data to the Immediate window.
    MyLine = MyLine & Input(1, #1)

Loop
Close #1    ' Close file.


Here is the contents of the .txt file...

1,Paul P. Barnes,Mastercard,1111222233334444-555,01/05,11223344556677
2,Customer1,Unspecified,,,,,22222,joe
3,001,10,Glass Bottle
3,002,1,Tubes
2,Cust2,,,,,,11111,ken@aol.com
3,001,30,Glass Bottle
3,002,1,Tubes


Thanks,

Ken
Avatar of Scott McDaniel (EE MVE )
Scott McDaniel (EE MVE )
Flag of United States of America image

Try this instead:

Do While Not EOF(1)  ' Loop until end of file.
    MyLocation = Loc(1)
    Input #1, ModeNum, CCName, CCType, CCNo, CCEXP, SUDef1
    Debug.Print ModeNum, CCName, CCType, CCNo, CCEXP, SUDef1    ' Print data to the Immediate window.
    MyLine = MyLine & Input(1, #1)
Loop
Avatar of kkapush
kkapush

ASKER

I tried it again and it does not work.  I receive the same error.

FYI, the process does parse through the entire .txt file line by line and when it gets past the last line it sends that message and does not exit the loop.  I'm not sure why.  There is something in the Microsoft help that explains to NOT USE the EOF function because it will give the "end of file" error I am experiencing.  It does suggest to use the Lof function and the Loc, but I cannot figure out that technique.

Thanks,

Ken
I've used the EOF function for years and don't receive the "end of file" error message unless I move past the end of the file ... do you know where the error is occurring? Which line?

I'm assuming you're referring to this from Help. If that's the case, rework your code to match the code below and try again.

Loc Function Example

This example uses the Loc function to return the current read/write position within an open file. This example assumes that TESTFILE is a text file with a few lines of sample data.

Dim MyLocation, MyLine
Open "TESTFILE" For Binary As #1    ' Open file just created.
Do While MyLocation < LOF(1)    ' Loop until end of file.
    MyLine = MyLine & Input(1, #1)    ' Read character into variable.
    MyLocation = Loc(1)    ' Get current position within file.
' Print to the Immediate window.
    Debug.Print MyLine; Tab; MyLocation
Loop
Close #1    ' Close file.



I've had files that that came off other systems (mainframes and such) that had characters that Access took to be EOF markers but were in the middle of the file.  The trick is to find out where they are (follow LSMConsulting advice above)  and then with a text editor (not notepad but a real text editor like PFE (http://www.lancs.ac.uk/staff/steveb/cpaap/pfe/default.htm) go to that location and do a search and replace for the that character.
Avatar of kkapush

ASKER

HI,

I tried the code you suggested and it yeilds the same error.

It goes through the loop 7 times and in the 8th time through is when the error occurs.  This makes sense since there is only 7 lines in the .txt file (see .txt file in the beginning of this question).  Why is the file continuing after the 7th time through?  It's not recognizing the end of the file.

Is there an End Of File character or set of characters that needs to be in the file??  

What is the line     "MyLine = MyLine & Input(1, #1)"     doing?

I'm not sure what this code does.  

Thanks,

Ken
ASKER CERTIFIED SOLUTION
Avatar of Jim P.
Jim P.
Flag of United States of America image

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 kkapush

ASKER

I have it!  The file I'm parsing has different amounts of data on each line.  I'm pulling 6 pieces of data each time through the loop.  I was assuming that the data is being parsed line by line no matter how many pieces of data per line.

It now works. Thanks everyone for your help.

Ken