• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 16. Error Handling: Preparing fo... > Creating a List of Error Codes and D...

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


					  


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint