Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

Tip of the Day
Language: Enterprise
Expertise: Beginner
Oct 19, 1999



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Display Contents of ADODB.Errors Collection

When ADO encounters an error, the Errors Collection is filled with certain specific details on the cause of the error. You can check the Errors collection of the Connection object whenever you encounter an error From an ADO object.
Here is a simple method which can be used to display the errors collection of Connection Object. Method GetErrorInformation() returns a single string which contains the complete error information. You can call this method from error handler of any method that makes use of ADODB objects as it is being called from CallADOMethods() 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
    ' ----------------------------------
    Exit Sub

    ' Display the complete error message
    MsgBox GetErrorInformation(oConnection.errors) & _
            vbCrLf, vbCritical + vbOKOnly, "ADO Errors"
End Sub

Private 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 = strErr

End Function
Deepak Pant
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date