ACC2000: Trapping Jet Errors with ADO and the OLE DB Provider

ID: Q201476


The information in this article applies to:

Advanced: Requires expert coding, interoperability, and multiuser skills.

This article applies only to a Microsoft Access database (.mdb).


SUMMARY

This article demonstrates how to trap Microsoft Jet database engine errors using ActiveX Data Objects (ADO) and the OLE DB provider for Microsoft Jet.


MORE INFORMATION

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact a Microsoft Certified Solution Provider or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Solution Providers, please see the following page on the World Wide Web:

http://www.microsoft.com/mcsp/
For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

http://www.microsoft.com/support/supportnet/overview/overview.asp
The sample code in this article uses Microsoft ActiveX Data Objects. For this code to run properly, you need to reference the Microsoft ActiveX Data Objects 2.1 Library.

To demonstrate the example code, follow these steps:
  1. Create a new Microsoft Access database.


  2. Create a new module.


  3. In the new module, type the following code:


  4. 
    Sub DemonstrateNativeErrors()
       Dim cn As New ADODB.Connection
       On Error GoTo ErrorHandler
    
       ' This code example demonstrates how the Jet OLE DB
       ' provider returns native errors to clients. The
       ' database this code is trying to open should not exist
       ' on the machine in order to produce an error for this demo.
    
       ' The error handler will work for all Jet errors that
       ' are returned to the client, regardless of the
       ' method or SQL statement that caused the error
    
       cn.Provider = "Microsoft.Jet.OLEDB.4.0"
       cn.Open "Data Source=c:\unknown.mdb"
    
       Exit Sub
    
    ErrorHandler:
    
       For i = 0 To cn.Errors.Count - 1
          Debug.Print _
             "Error Source (OLE DB Component Returning Error): " _
             & cn.Errors(i).Source
          Debug.Print "Error Description: " & cn.Errors(i).Description
          Debug.Print "OLE DB Hexadecimal Error Value (HRESULT):" _
             & " 0x" & Hex(cn.Errors(i).Number)
          ' Handle native error:  This functionality is specific to
          ' the native Jet OLE DB provider. Other providers will have
          ' different formats for their native errors, if supported at
          ' all. The native Jet OLE DB provider returns two signed,
          ' 16-bit values in the NativeError value (which is natively a
          ' 32-bit unsigned long). The low word is the major error.
          ' The high word is the minor error. These values are
          ' "sign-descended" to fit into 16 bits and stored together in
          ' the 32-bit unsigned value.
    
          ' These are the errors internally used by Jet and are
          ' different than the errors returned by the Data Access
          ' Object (DAO.) Please see below for how DAO's errors are
          ' exposed. In some cases, Jet has no minor error code for the
          ' particular failure. In these  cases, the major error will
          ' be duplicated into the upper word.
    
          n = cn.Errors(i).NativeError
          Dim uword As Long, lword As Long
    
          If (n < 0) Then
             sign = -1
          Else
             sign = 1
          End If
    
          lword = (Abs(n) And &HFFFF&) * sign
          uword = (Abs((n / 2 ^ 16 - 1)) And &HFFFF&) * sign
          Debug.Print "Minor Error: " & uword
          Debug.Print "Major Error: " & lword
    
          ' The IDA is a string resource value. Multiple Jet errors
          ' may map into a single IDA error. Therefore, the native Jet
          ' errors may provide an additional level of granularity when
          ' writing applications to the native Jet OLE DB provider
    
          Debug.Print "Jet IDA Number (DAO Error): " _
              & cn.Errors(i).SQLState
    
          ' SQLState is actually a SQL92 error reporting construct that
          ' isn't used by the Jet database engine. The DAO values are
          ' provided for backward compatibility with existing and
          ' migrating applications. In general, clients will want to
          ' use internal errors instead of IDA/DAO errors.
    
       Next i
    
       Resume Next
    
    End Sub 
  5. Press CTRL+G to display the Immediate window.


  6. In the Immediate window, type the following and press ENTER:
    
    DemonstrateNativeErrors 
    You next see the following output in the Immediate window.


  7. 
       Error Source (OLE DB Component Returning Error):
       Microsoft JET Database Engine
       Error Description: Could not find file 'c:\unknown.mdb'.
       OLE DB Hexadecimal Error Value (HRESULT): 0x80004005
       Minor Error: -8158
       Major Error: -1811
       Jet IDA Number (DAO Error): 3024 

Additional query words: inf


Keywords          : kbdta AccCon KbVBA 
Version           : WINDOWS:2000
Platform          : WINDOWS 
Issue type        : kbinfo 

Last Reviewed: July 6, 1999