没有人真正回答了你的问题。
假设你的代码像这样(一个基本框架):
Public Sub MySub()
On Error GoTo errHandler
Dim rs As DAO.Recordset
Set rs = CurrentDB.OpenRecords([SQL SELECT])
If rs.RecordCount >0 Then
rs.MoveFirst
Do Until rs.EOF
[do whatever that produces the error]
errSkipToNext:
rs.MoveNext
Loop
End If
exitRoutine:
If Not (rs Is Nothing) Then
rs.Close
Set rs = Nothing
Exit Sub
errHandler:
Select Case Err.Number
Case X, Y, Z ' where these are error numbers you want to ignore
Err.Clear
' do whatever it is you need to do in order to record the offending row
Call RecordError(rs!PK, Err.Number) ' PK is a field that identifies the bad record
GoTo errSkipToNext
Case Else
MsgBox Err.Number & ": " & Err.Description, vbExclamation, _
"Error!"
Resume exitRoutine
End Select
End Sub
在这段代码中,您在错误处理程序中使用SELECT CASE来决定要忽略哪些错误。在我上面的代码框架中,我将错误编号列为
X,Y,Z
,但您应该将其替换为您想要忽略的实际错误编号。
您不希望忽略每个错误,因为您可能会在子例程的其他地方忽略重要错误。如果您不想弄清楚要忽略的有限数量的错误是什么,我建议您在产生要忽略的错误的代码块的开头设置一个标志,然后使用“如果bolErrorInCodeBlockToIgnore Then”来决定是否忽略所有错误。类似这样:
Public Sub MySub()
On Error GoTo errHandler
Dim rs As DAO.Recordset
Dim bolErrorInCodeBlockToIgnore As Boolean
Set rs = CurrentDB.OpenRecords([SQL SELECT])
If rs.RecordCount >0 Then
rs.MoveFirst
Do Until rs.EOF
bolErrorInCodeBlockToIgnore = True
[do whatever that produces the error]
errSkipToNext:
rs.MoveNext
Loop
End If
exitRoutine:
If Not (rs Is Nothing) Then
rs.Close
Set rs = Nothing
Exit Sub
errHandler:
If bolErrorInCodeBlockToIgnore Then
Err.Clear
' do whatever it is you need to do in order to record the offending row
Call RecordError(rs!PK, Err.Number) ' PK is a field that identifies the bad record
bolErrorInCodeBlockToIgnore = False
GoTo errSkipToNext
Else
MsgBox Err.Number & ": " & Err.Description, vbExclamation, _
"Error!"
Resume exitRoutine
End If
End Sub
我更喜欢第一种方式,因为我坚信只忽略已知错误,而不是发生的任何旧错误。但是,可能很难想出能生成所有您想要忽略的可能错误的测试。
On Error GoTo...
,那么我预计会收到这个错误信息。问题在于我的代码出现了错误,而不是转到我设置的标签。 - rdevitt