ACC2: Trapping Specific ODBC Error Messages

Last reviewed: May 20, 1997
Article ID: Q129165
The information in this article applies to:
  • Microsoft Access version 2.0

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


Keywords : kbusage OdbcSqlms
Version : 2.0
Platform : WINDOWS
Hardware : X86
Issue type : kbinfo


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: May 20, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.