2017-04-16 109 views
1

我必须捕捉with块内的错误。我正在读取一个文件,并将其添加到表格中,然后将这些记录添加到表格中。如果出现任何错误移到下一行文件On Error not in vba

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 
+0

看看[这里](http://stackoverflow.com/a/41553765/4926357)或者[这里](HTTP:// stackoverflow.com/a/42457284/4926357) –

+0

@ASH已完成 – Faisal

+0

如果此块位于循环内部,您可能错误地处理了错误,请尝试显示一些循环的代码。 –

回答

3

当程序内发生错误时,你不能只是循环并与另一On Error重新启动。规则是:

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

你的循环更改为这样的事情:

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 
+2

您还应该在代码中您希望发生错误的部分之后放置“On Error Goto 0”。 –

+0

@RichHolton这取决于例程中是否还有其他相关部分。 –

+0

@ A.S.H谢谢你完美的作品。你能否也请回答这个问题http://stackoverflow.com/questions/43439579/how-to-set-column-values-in-a-record-set-in-access-vba – Faisal

相关问题