The DBEngine has an Errors collection that can be manipulated by the FOR-
EACH construct. The JET Engine can store multiple errors in the DBEngine
Errors collection. In Visual Basic 3.0, it was possible to parse the string
using the routine shown on Page 175 of the Visual Basic 4.0 Professional
Features Book under the "Guide to Data Access Objects" section. The #
symbol was used to separate the "ODBC Call Failed" message from the
detailed ODBC description in Visual Basic 3.0. However, this is not
necessary under Visual Basic versions 4.0 and 5.0.
For the example below, a two-field table called MyTable has been set up on
an ODBC Source and a primary key set on the ID Field. Two records have been
added as below:
Field ID Description
===============================
Record 1 1 Hello
Record 2 2 World
The code below will generate an error by trying to add a record with a
duplicate primary key value to test the code:
- Start a new Standard EXE project. Form1 is added by default.
- Add a CommandButton to Form1.
- Add the following code to the General Declarations section of Form1:
Option Explicit
Private Sub Command1_Click()
Dim db As Database
Dim rs As Recordset
On Error GoTo trap
Set db = OpenDatabase("")
Set rs = db.OpenRecordset("Select * from MyTable")
rs.AddNew
rs.Fields(0).Value = 2
rs.Update
Exit Sub
trap:
MsgBox Errors.Count
MsgBox Err.Number & " " & Err.Description
End Sub
- Press the F5 key to run the project. Click on the CommandButton and you
should receive error 3146, "ODBC Call Failed." Although the Error count
is greater than one, only one message will be displayed.
- Remove the code from within the error trap and replace it with one
of the following error handlers:
' DAO Error Handler
Dim MyError As Error
MsgBox Errors.Count
For Each MyError In DBEngine.Errors
With MyError
MsgBox .Number & " " & .Description
End With
Next MyError
' RDO Error Handler
Dim MyError As rdoError
MsgBox rdoErrors.Count
For Each MyError In rdoEngine.rdoErrors
With MyError
MsgBox .Number & " " & .Description
End With
Next MyError
- Press the F5 key to run the project. You should see a detailed message
and then the 3146 Error for "ODBC Call Failed."