Using the Errors Collection
The Errors collection is part of Access's Jet Engine. It stores the most recent set of DAO errors that have occurred. This is important when dealing with DAO (Data Access Objects) and ODBC (Open Database Connectivity), in which one operation can result in multiple errors. If you are concerned with each error generated by one operation, you need to look at the Errors collection. Each error object in the Errors collection contains information about an error that occurred. If you want to view the errors stored in the Errors collection, you must loop through it, viewing the properties of each Err object. Listing 16.12 shows the code you can use to accomplish this.
Listing 16.12 Viewing the Errors Stored in the Errors Collection
Sub TestErrorsCollection()
'Invoke error handling
On Error GoTo TestErrorsCollection_Err
'Declare a DAO database object
Dim db As DAO.Database
'Point the database object at the database
'referenced by the CurrentDB object
Set db = CurrentDb
'Attempt to execute a query that doesn't exist
db.Execute ("qryNonExistent")
Exit Sub
TestErrorsCollection_Err:
Dim ErrorDescrip As DAO.Error
'Loop through the Errors collection,
'sending the error number and description to
'the Immediate window
For Each ErrorDescrip In Errors
Debug.Print ErrorDescrip.Number
Debug.Print ErrorDescrip.Description
Next ErrorDescrip
Exit Sub
End Sub
This routine loops through each Error object in the Errors collection, printing the description of each error contained in the collection.