Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


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

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
    ' ----------------------------------
    oConnection.Close
    Exit Sub

CallADOMethodsErrorHandler:
    ' 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.

 

 

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