access-vba Trap All Errors


Basic error handling should be added to all procedures in production code, since otherwise an unexpected error will cause Access to crash or invite the end-user to enter debug mode.

A typical pattern for a basic error handler that traps all errors is:

Sub Name()
On Error GoTo errHandler

'[some code...]

    'Clean up any objects
    Exit Sub

    Select Case Err.Number
        'Any expected error cases go here
    Case Else
        MsgBox Err.Number & Err.Description
    End Select
    Resume exitHandler   'Make sure objects get cleaned up when code threw an error
End Sub

For a more detailed discussion of error handling in Access VBA, see: