沒有人真的回答了你的問題。
說你的代碼是這樣的(骨骼的框架):
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