Error and Exception Handling

Analysis Services Programming

Analysis Services Programming

Error and Exception Handling

The first error trapped by Microsoft® Visual Basic® can be ambiguous if you are working with Microsoft ActiveX® Data Objects (ADO), and if the error is displayed alone. You should retrieve any additional error information. ADO provides an additional layer for handling exceptions that result from data operations. You can use the Errors collection of the Connection object to retrieve information about more than one kind of error.

Examples
A. Using the ADO Connection Object

The following code example introduces an error into the connection string of the Connection object. This error induces two new errors in addition to the standard error passed through by the Visual Basic Err object. These errors are assembled into a single string.

    Dim sErrDesc As String
    Dim erCur As Error
    Dim cn As New ADODB.Connection
    
    ' Define a connection to an object that does not exist.
    cn.ConnectionString = "Provider=msolap; Datasource=NoSuchServer;"
    ' This provides two errors in the ADO errors collection.
    
    On Error GoTo found_error
     
    cn.Open

    ' Because a computer named NoSuchServer does not exist,
    ' you should never reach this point.
    Exit Function
    
found_error:
    
    ' Keep Visual Basic error description -- On Error Resume Next clears it.
    sErrDesc = Err.Description & vbCrLf
    
    On Error Resume Next

    ' Get the ADO errors.
    If cn.Errors.Count > 0 Then
        For Each erCur In cn.Errors
            sErrDesc = sErrDesc & erCur.Source & ": " & erCur.Description & vbCrLf
        Next erCur
    End If
    MsgBox sErrDesc