devxlogo

Display Contents of ADODB.Errors Collection

Display Contents of ADODB.Errors Collection

When ADO encounters an error, the Errors Collectionis filled with certain specific details on the causeof the error. You can check the Errors collection ofthe Connection object whenever you encounter an errorFrom an ADO object.
Here is a simple method which can be used to displaythe errors collection of Connection Object. MethodGetErrorInformation() returns a single string whichcontains the complete error information. You can callthis method from error handler of any method that makesuse of ADODB objects as it is being called fromCallADOMethods() method.

 Private Sub CallADOMethods()    On Error GoTo CallADOMethodsErrorHandler    Dim oConnection  As ADODB.Connection    ' Create a new Connection    Set oConnection = New ADODB.Connection    ' Open the connection    oConnection.Open "DSN=Sample"    ' ----------------------------------    ' Calls to other ADO objects go here    ' ----------------------------------    oConnection.Close    Exit SubCallADOMethodsErrorHandler:    ' Display the complete error message    MsgBox GetErrorInformation(oConnection.errors) & _            vbCrLf, vbCritical + vbOKOnly, "ADO Errors"End SubPrivate Function GetErrorInformation( _            ByVal oErrorColl As ADODB.errors) As String    ' Count of errors in oErrorColl    Dim lErrorCount As Long    ' Index into oErrorColl    Dim lErrorIndex As Long    ' Error Object    Dim oError      As ADODB.Error    ' Errors concatenated to one single string    Dim strErr    As String    ' Get the count of errors in error collection    lErrorCount = oErrorColl.Count    If (lErrorCount > 0) Then        strErr = "Errors reported by ADO" & vbCrLf    End If    For lErrorIndex = 0 To (lErrorCount - 1)    ' Get the error object at index lErrorIndex        Set oError = oErrorColl.Item(lErrorIndex)        With oError            strErr = strErr & "(" & lErrorIndex + 1 & ") "            strErr = strErr & "Error#: " & .Number & vbCrLf            strErr = strErr & vbTab & "Desc. : " & _                        .Description & vbCrLf            strErr = strErr & vbTab & "Source: " & _                        .Source & vbCrLf            strErr = strErr & vbTab & "Native Error: " & _                        .NativeError & vbCrLf            strErr = strErr & vbTab & "SQL State: " & _                        .SQLState & vbCrLf            strErr = strErr & vbTab & "Help Context: " & _                        .HelpContext & vbCrLf            strErr = strErr & vbTab & "Help File: " & _                        .HelpFile & vbCrLf        End With    Next lErrorIndex    ' Return the complete error string    GetErrorInformation = strErrEnd Function
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist