Determine the Error Codes Reserved by Visual Basic

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

The following procedure creates a table in Microsoft Access containing the error codes and strings used or reserved by Visual Basic. The table doesn't include ActiveX Data Objects (ADO) errors.

Visual Basic reserves a portion of the first thousand possible error numbers, so this example considers only error numbers between 1 and 1000. Other error numbers are reserved by the Microsoft Jet database engine, or are available for defining custom errors.

Sub CreateErrorsTable()
    Dim cat As New ADOX.Catalog
    tbl As New ADOX.Table
    cnn As ADODB.Connection
    Dim rst As New ADODB.Recordset, lngCode As Long
    Const conAppObjectError = "Application-defined or object-defined error"

    Set cnn = CurrentProject.Connection
    ' Create Errors table with ErrorNumber and ErrorDescription fields.
    tbl.Name = "Errors"
    tbl.Columns.Append "ErrorCode", adInteger
    tbl.Columns.Append "ErrorString", adVarWChar
    Set cat.ActiveConnection = cnn
    cat.Tables.Append tbl
    ' Open recordset on Errors table.
    rst.Open"Errors", cnn adOpenStatic, adLockOptimistic
    ' Loop through first 1000 Visual Basic error codes.
    For lngCode = 1 To 1000
        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!ErrorCode = Err.Number
            rst!ErrorString = Err.Description
        End If
        ' Clear Err object.
    Next lngCode
    ' Close recordset.
    DoCmd.Hourglass False
    MsgBox "Errors table created."
End Sub