Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

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


View products that this article applies to.

This article was previously published under Q201476
Advanced: Requires expert coding, interoperability, and multiuser skills.

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

↑ Back to the top


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.

↑ Back to the top


More information

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers 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 requirements. 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:
    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
    					
  4. Press CTRL+G to display the Immediate window.
  5. In the Immediate window, type the following and press ENTER:
    DemonstrateNativeErrors
    						
    You next see the following output in the Immediate window.
       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
    					

↑ Back to the top


Keywords: KB201476, kbinfo

↑ Back to the top

Article Info
Article ID : 201476
Revision : 3
Created on : 6/23/2005
Published on : 6/23/2005
Exists online : False
Views : 351