Determine the Error Codes Reserved by Visual Basic

Determine the Error Codes Reserved by Visual Basic

See Also

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.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