2017-04-16 112 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

相關問題