没有人真的回答了你的问题。
说你的代码是这样的(骨骼的框架):
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然后决定是否忽略所有错误。事情是这样的:
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
我更倾向于第一种,因为我只知道忽略的错误,而不是发生任何旧的错误是一个坚定的信徒。但是想出可能会产生所有可能的错误的测试可能相当困难。
你能提供一个你看到的错误信息的例子吗? – 2011-04-28 17:12:54
错误消息不相关。该错误与我的数据库字段的格式有关,并且这是一个错误消息,如果未设置“On Error GoTo ...”,我会期望得到该错误消息。这是事实,我的代码打破了,而不是去我设置的标签。 – rdevitt 2011-04-29 02:46:10
我问的原因是因为在Access中触发了一些无法使用VBA捕获的错误消息。 – 2011-04-29 12:13:26