Kaja
Kaja

Reputation: 3057

how to fix Run time error 62 ''Input past end of file'

I have a problem while trying to read a text file. Basically, the text file is comprised of blocks of information, between each block I have a blanck row. Hier is a sample of my text:

    FESTWERT FRAUS 
       LANGNAME "bla bla bla" 
       FUNKTION dfgg 
       EINHEIT_W "ü"
       WERT -9.2654122070312500
   END

    KENNFELD KFDWNWCSA 4 4
       LANGNAME "bla bla bla" 
       FUNKTION FGHK 
       EINHEIT_X "8/kl"
       EINHEIT_Y "bla"
       EINHEIT_W "bla"
       ST/X   1658.0000000000000000   987.0000000000000000   3698.0000000000000000           3520.0000000000000000   
       ST/Y   -30.0000000000000000
       WERT   22.0000000000000000   16.9870000000000000   10.3210000000000000    10.0000000000000000   
       ST/Y   0.0000000000000000
       WERT   10.0000000000000000   10.0000000000000000   10.0000000000000000   10.0000000000000000   
       ST/Y   45.2500000000000000
       WERT   10.0000000000000000   10.0000000000000000   10.0000000000000000   10.0000000000000000   
       ST/Y   21.0000000000000000
       WERT   22.0000000000000000   16.0000000000000000   10.0000000000000000   10.0000000000000000   
    END

actually I want to extract the numbers from these blocks. but I get this error: this is my Code:

  Dim fso As New FileSystemObject
 Dim ts As TextStream
 Set ts = fso.OpenTextFile(Name, ForReading)
 Do While Not ts.AtEndOfStream

 strArray = Split(ts.ReadLine, Space(1), 2) ' Extrahieren, was in einer Zeile ist

If Len(Join(strArray, "")) <> 0 Then 
   If strArray(0) = "KENNFELD" Then  
     SWKNF = True
     ts.SkipLine
     ts.SkipLine
     ts.SkipLine
     ts.SkipLine
     ts.SkipLine
     wertkenfeld = strArray(1)
     strArray(1) = ""
   End If


   If strArray(0) = "END" Then werden
     If SWKNFL = True Then 
      For P = 0 To X - 1
        DoCmd.RunSQL ("INSERT INTO Test_DCML_G (XValue,Wert,name) VALUES ('" & Stx(P) & "','" & wert(P) & "','" & wertkenfeld & "');")
      Next P
     End If
     SWKNF = False
     SWKNFL = False
     Erase Warray
     X = 0
     W = 0
     Erase Yarray
     Erase Xarray
     Erase Stx
     Erase wert
     ts.SkipLine ' I get the error in this line
   End If
.....

Would you please help me to solve this problem? Thank you so much

Upvotes: 1

Views: 13114

Answers (1)

Wiz
Wiz

Reputation: 121

When you reach the last END of the file, a skip or read takes you beyond then end of the file.

I suggest you, as a general rule, if you need to skip or read lines to check EOF before moving (both for text files or recordset). You can also use a loop with a condition in which you test if EOF has not been reached or the desired line has been found.

The first chunk in which you skip lines could be written as

  strReadLine = ""
  do while not ts.AtEndOfStream and not (strReadLine LIKE "ST/*")
    strReadLine = ts.readline
  loop

Bye

Upvotes: 2

Related Questions