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
Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several