Creating a List of Error Codes and Descriptions
Many people ask me how to create a list of error numbers and descriptions. The code in Listing 16.37 creates a table of all the errors that can occur in your VBA code, with a description of what each error number means. You can copy this code into any module and run it.
Listing 16.37 Code That Creates a Table of Errors and Descriptions
Sub CreateErrorsTable()
Dim cnn As ADODB.Connection
Dim rst As New ADODB.Recordset
Dim lngCode As Long
Const conAppObjectError = "Application-defined or object-defined error"
Set cnn = CurrentProject.Connection
' Open recordset on Errors table.
rst.Open "tblErrorMessages", cnn, adOpenStatic, adLockOptimistic
' Loop through first 10000 Visual Basic error codes.
For lngCode = 1 To 10000
On Error Resume Next
' Raise each error.
Err.Raise lngCode
DoCmd.Hourglass True
' Skip error codes that generate application or object-defined errors.
If Err.Description <> conAppObjectError Then
' Add each error code and string to Errors table.
rst.AddNew
rst!ErrorCode = Err.Number
rst!ErrorString = Err.Description
rst.Update
End If
' Clear Err object.
Err.Clear
Next lngCode
' Close recordset.
rst.Close
DoCmd.Hourglass False
MsgBox "Errors table created."
End Sub
The code opens a recordset based on the tblErrorMessages table. It loops through from 1 to 10000, raising an error with each number. Each time through the loop, it appends the error number and the associated error description to the tblErrorMessages table.