ACC2: Trapping Specific ODBC Error MessagesID: Q129165
|
Advanced: Requires expert coding, interoperability, and multiuser skills.
This article demonstrates how to create a sample user-defined Access
Basic function you can use to trap and parse a specific Open Database
Connectivity (ODBC) error message, such as the error message returned by
Microsoft SQL Server if you duplicate a unique index.
ODBC error strings are returned in two parts. The first part is the generic
"ODBC call failed" error message; the second part is the specific error
message. If you use the Err() function to trap only the error number (which
is 3146 for a duplicate unique index), you trap only the first part, the
generic "ODBC call failed" error message. To get the specific error
message, you must parse the full error string that is returned.
This article assumes that you are familiar with Access Basic and with
creating Microsoft Access applications using the programming tools provided
with Microsoft Access. For more information about Access Basic, please
refer to the "Building Applications" manual.
Function TrapODBCError ()
On Error GoTo ErrorHandler
Dim mydb As Database
Dim myrs As Recordset
Dim ErrorString As String, ErrorNum As Double
Dim ErrorStart As Integer, ErrorLength As Integer
Set mydb = CurrentDB()
Set myrs = mydb.OpenRecordset("dbo_authors", db_open_dynaset,_
db_appendonly)
myrs.AddNew
myrs.au_id = "xxx"
myrs.au_lname = "Smith"
myrs.contract = 0
myrs.Update
Exit Function
ErrorHandler:
ErrorString = Error$
'Find the beginning of the error string. It begins with "#."
ErrorStart = InStr(1, ErrorString, "#") + 1
'Find the length of the error value.
ErrorLength = InStr(ErrorStart, ErrorString, ")") - ErrorStart
'Get the error number.
ErrorNum = Mid(ErrorString, ErrorStart, ErrorLength)
MsgBox "Error number " & CStr(ErrorNum) & " has occurred."
Exit Function
End Function
Microsoft Access "Building Applications," version 2.0, Chapter 10, "Handling Run-Time Errors," pages 221-238
Keywords : kbusage OdbcSqlms
Version : 2.0
Platform : WINDOWS
Issue type : kbinfo
Last Reviewed: April 9, 1999