嘿我正在使用此代码从Excel文件更新Access表。
On Error GoTo Boucle2
row = 1 ' the start row in the worksheet
Do While Not IsEmpty(Worksheets("Temp").Range("A" & row))
With adoRec
If Worksheets("Temp").Range("A" & row).Value = "Processed" Then
.AddNew
.Fields("TRADE_ID") = Worksheets("Temp").Range("B" & row).Value
.Fields("Tkt") = Worksheets("Temp").Range("C" & row).Value
.Update
ElseIf Worksheets("Temp").Range("A" & row).Value = "AmendValid" Then
adoRec.Filter = "TRADE_ID='" & Worksheets("Temp").Range("B" & row).Value & "'"
.Fields("Tkt") = Worksheets("Temp").Range("C" & row).Value
.Update
End If
End With
Boucle2 :
row = row + 1
Loop
我想处理一个常见的错误....如果TRADE_ID
已经存在...我得到一个正常的Runtime Error '-2147217887 (80040e21)'
,表示此TRADE_ID已经存在且不支持重复。
我想通过转到循环中的下一行来忽略此错误,并且可能添加一个msgbox说This TRADE_ID already exists
非常感谢
答案 0 :(得分:1)
试试这个:
Sub test()
'some code
On Error GoTo errHandler
Do While Not IsEmpty(Worksheets("Temp").Range("A" & Row))
With adoRec
'add record
End With
Boucle2:
Row = Row + 1
Loop
'some code
Exit Sub
errHandler:
If Err.Number = -2147217887 Then
MsgBox "This TRADE_ID already exists"
Else
MsgBox "Unknown error: " & Err.Description
End If
Resume Boucle2
End Sub
答案 1 :(得分:0)
这将处理该错误 - 您可以自定义以处理其他错误。
On Error GoTo Error_Trap
Row = 1 ' the start row in the worksheet
Do While Not IsEmpty(Worksheets("Temp").Range("A" & Row))
With adoRec
If Worksheets("Temp").Range("A" & Row).Value = "Processed" Then
.AddNew
.Fields("TRADE_ID") = Worksheets("Temp").Range("B" & Row).Value
.Fields("Tkt") = Worksheets("Temp").Range("C" & Row).Value
.Update
ElseIf Worksheets("Temp").Range("A" & Row).Value = "AmendValid" Then
adoRec.Filter = "TRADE_ID='" & Worksheets("Temp").Range("B" & Row).Value & "'"
.Fields("Tkt") = Worksheets("Temp").Range("C" & Row).Value
.Update
End If
End With
Row = Row + 1
Loop
Exit Function
Error_Trap:
If Err.Number = -2147217887 Then
MsgBox "When processing Row: " & Row & " - Record already exists for Ticket: " & Worksheets("Temp").Range("C" & Row).Value & vbCrLf & vbCrLf & _
"Will skip this record.", vbOKOnly, "Duplicate Record"
Resume Next
Else
MsgBox "Error Encountered - what do you want to do? Add Code..." & vbCrLf & vbCrLf & _
"Err: " & Err.Number & vbTab & Err.Description, vbOKOnly, "Error"
Resume Next
End If