XL5: Visual Basic Example Using the SQLError Function |
This article explains how to use the SQLError function in a Visual Basic, Applications Edition, macro in Microsoft Excel version 5.0.
In Microsoft Excel version 5.0, the XLODBC.XLA add-in file includes 7 SQL
functions for accessing external data sources with ODBC (Open Database
Connectivity) drivers. One of these functions, SQLError, returns detailed
error information when you call it after one of the other ODBC SQL
functions fails. If SQLError itself fails, it cannot return any error
information.
If an ODBC function such as SQLExecQuery fails, the error information
is defined and stored in memory. You can then make a call to the
SQLError function and assign this information to an array variable.
The information returned by SQLError has three formats:
Sub SQLError_Example()
'This example will return an error because
'the data source being opened doesn't exist.
'Declare variables
Dim Channel As Variant 'Channel number
Dim MyErrorArray As Variant 'Variable to store SQLError array
Dim LF As String 'Line Feed variable
LF = Chr(10) 'Assign value for Line Feed
'Open a channel to the Wind data source. This line will
'produce an error because the data source 'Wind' doesn't exist.
Channel = SQLOpen("DSN=Wind")
'Check for possible error value in the Channel variable.
If IsError(Channel) Then
'Assign the elements of the SQLError array to MyErrorArray.
MyErrorArray = SQLError()
'Display the individual error elements of the MyErrorArray array.
'Add Line Feed character for readability.
MsgBox MyErrorArray(1) & LF & MyErrorArray(2) & LF & MyErrorArray(3)
'Exit sub procedure
Exit Sub
End If
'Close the channel to Wind.
SQLClose Channel
End Sub
When you run this macro, you will receive an error message similar to the
following:
IM002 0 [Microsoft][ODBC DLL] Data source not found and no default driver specified
"Visual Basic User's Guide," version 5.0, Chapter 9, "Using the
Built-in Error Values of Microsoft Excel"
For more information about SQLError, choose the Search button in
Programming With Visual Basic Help, and type:
SQLError Function
Additional query words: Isnumeric
Keywords :
Version : WINDOWS:5.0,5.0c
Platform : WINDOWS
Issue type :
Technology :
|
Last Reviewed: October 1, 1999 © 2000 Microsoft Corporation. All rights reserved. Terms of Use. |