Access Common Error Handler
The VBA code illustrates a common error handler technique. Keep in mind there are three forms of the On Error Statement:
(1) On Error GoTo Line (Usually An Error Handling Routine)
(2) On Error GoTo 0 (Turns Off User Error Handling)
(3) On Error Resume Next (Ignores the Error And Executes The Next Statement
For a list of all error codes, see VBA Error Codes.
Program Code
' **************************************************
' Module 1 - This Creates An Error Condition
' **************************************************
Option Compare Database
Option Explicit
Sub TestError()
Dim intDividend As Integer
Dim intDivisor As Integer
Dim intResult As Integer
On Error GoTo ErrorHandler
intResult = intDividend / intDivisor
Exit_Procedure:
' *********************************************
' Close Files, Set Objects to Nothing
' *********************************************
DoCmd.SetWarnings True
DoCmd.Hourglass False
On Error GoTo 0
Exit Sub
ErrorHandler:
CommonErrorHandler Err.Number, Err.Description
Resume Exit_Procedure
End Sub
' **************************************************
' Module 2 - This Displays The Error Message
' **************************************************
Option Compare Database
Option Explicit
Public Sub CommonErrorHandler(ErrorNumber As String, ErrorDescription As String)
MsgBox "An Error Occurred In This Application" & vbCrLf & _
"Please Contact The Developer" & vbCrLf & vbCrLf & _
"Error Number = " & ErrorNumber & " Error Description = " & _
ErrorDescription, vbCritical
End Sub
