如何修复运行时错误62''输入文件末尾'

时间:2013-05-27 09:10:44

标签: vba ms-access access-vba ms-access-2010

我在尝试阅读文本文件时遇到问题。基本上,文本文件由信息块组成,在每个块之间我有一个blanck行。 Hier是我的文字样本:

    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

实际上我想从这些块中提取数字。但是我得到了这个错误:这是我的代码:

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

请你帮我解决这个问题? 非常感谢你

1 个答案:

答案 0 :(得分:2)

当你到达文件的最后一个END时,跳过或读取将超过文件末尾。

我建议您,作为一般规则,如果您需要跳过或读取行以在移动之前检查EOF(对于文本文件或记录集)。 您还可以使用一个循环,其中包含测试是否尚未到达EOF或找到所需行的条件。

跳过行的第一个块可以写为

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

再见