ACC2: Trapping Specific ODBC Error Messages (129165)



The information in this article applies to:

  • Microsoft Access 2.0

This article was previously published under Q129165

SUMMARY

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.

MORE INFORMATION

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.

Creating the Sample Function

To create the function that traps the specific error message, create a new module and enter the following procedure.

NOTES:
  • The following sample code assumes that you have an attached table called dbo_authors from the sample database PUBS in Microsoft SQL Server.
  • In the following sample code, an underscore (_) at the end of a line is used as a line-continuation character. Remove the underscore from the end of the line when re-creating this code.
        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
    						

REFERENCES

Microsoft Access "Building Applications," version 2.0, Chapter 10, "Handling Run-Time Errors," pages 221-238

Modification Type:MajorLast Reviewed:11/6/2000
Keywords:kbinfo kbusage KB129165