Microsoft Knowledge Base Article
This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved.
Terms
of Use |
Trademarks
Article ID: 201476 - Last Review: June 23, 2005 - Revision: 3.0
ACC2000: Trapping Jet Errors with ADO and the OLE DB Provider
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).
This article demonstrates how to trap Microsoft Jet database engine errors using ActiveX Data Objects (ADO) and the OLE DB provider for Microsoft Jet.
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:
- Create a new Microsoft Access database.
- Create a new module.
- 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
- Press CTRL+G to display the Immediate window.
- In the Immediate window, type the following and press ENTER:
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
APPLIES TO
- Microsoft Access 2000 Standard Edition
Community Feedback System
Very often, it takes hours to solve a problem. Very often, you've looked high
and low, and have tried a lot of solutions. When you finally found it, chances
are, it was because someone else helped you. Here's your chance to give back.
Use our community feedback tool to let others know what worked for you and what
didn't.
Please also understand that the community feedback system is not warranted to be
correct, it's simply a system that we've built to let people try and help each
other. If something in a feedback response doesn't make sense to you, or you're
not comfortable making changes that the feedback talks about (like registry
edits), please consult a professional.
Thank you for using kbAlertz.com Feedback System.
-- Scott Cate