Article ID: 201476
Article Last Modified on 6/23/2005
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
DemonstrateNativeErrorsYou 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
Additional query words: inf
Keywords: kbinfo KB201476