出错时无法在vba中运行

时间:2017-04-16 19:31:17

标签: vba ms-access access-vba

我必须在with块中捕获错误。我正在读取一个文件,该文件被删除,然后在表格中添加记录。如果发生任何错误,请移至文件

中的下一行
    Do Until EOF(1)
    Line Input #1, strTextLine

'regex.replaces will replace the commas outside quotes with <???> and then the Split function will split the result based on our replacement
    regex.Pattern = ",(?=([^""]*""[^""]*"")*(?![^""]*""))"
    strArray = Split(regex.Replace(strTextLine, "<???>"), "<???>")
    Set rs = db("ATC").OpenRecordset
    With rs
        .AddNew
        On Error GoTo lpp
        !ATC_ID = Replace(strArray(0), """", "")
        !ATC_NAME = Replace(strArray(1), """", "")
        .update
        .Close
    End With
 lpp:
     'goto next line in the file
     Loop

1 个答案:

答案 0 :(得分:3)

当例程中发生错误时,您不能只使用其他On Error循环并重新启动。规则是:

  

在设置另一个Resume之前,您必须至少调用一次关键字On Error

将循环更改为:

    Do Until EOF(1)
        ' ...
        On Error GoTo ErrHandler
        ' ...

lpp:
    Loop
    Exit Sub
ErrHandler:
    Resume lpp ' <-- make sure Resume is invoked before proceeding with the loop