The Errors Collection

The first thing to remember about the DAO Errors collection is that it is not the same as the VBA.Err object. The VBA.Err object is a single object that stores information about the last VBA error. The DAO Errors collection stores information about the last DAO error.

Any operation performed on any DAO object can generate an error. The DBEngine.Errors collection stores all the error objects that are added as the result of an error that occurs during a single DAO operation. Each Error object in the collection, therefore, contains information about only one error.

Having said that, some operations can generate multiple errors, in which case the lowest level error is stored in the collection first, followed by the higher level errors. The last error object usually indicates that the operation failed. Enumerating the Errors collection allows your error handling code to more precisely determine the cause of the problem, and to take the most appropriate remedial action.

When a subsequent DAO operation generates an error, the Errors collection is cleared and a new set of Error objects is added to the collection. This happens regardless of whether you have retrieved the previous errors' information or not. So you can see that unless you retrieve the information about an error as soon as it occurs, you may lose it if another error happens in the meantime. Each error obliterates and replaces its predecessor—a bit like politics really.

One last point to note is that an error that occurs in an object that has not yet been added to its collection, is not added to the DBEngine.Errors collection, because the "object" is not considered to be an object until it is added to a collection. In such cases, the error information will be available in the VBA.Err object.

To fully account for all errors, your error handler should verify that the error number returned by both the VBA.Err object and the last member of the DBEngine.Error object are the same. The following code demonstrates a typical error handler:

intDAOErrNo = DBEngine.Errors(DBEngine.Errors.Count -

- 1).Number

If VBA.Err <> intDAOErrNo Then

DBEngine.Errors.Refresh End If

For intCtr =0 To DBEngine.Errors.Count - 1

Select Case DBEngine.Errors(intCtr).Number Case 1

'Code to handle error Case 2

'Code to handle error

'Other Case statements

'Code to handle error End Select Next intCtr

0 0

Post a comment