Microsoft Office 2000/Visual Basic Programmer's Guide |
Logging errors is the process of recording information about an error. You can use error-logging techniques to help debug your application. And even though you hope that most bugs are removed from your code before you deploy your application to your users, you may consider using error-logging techniques in "finished" applications as well.
One powerful option to consider is to create your own error-logging object class that you can use in any application by simply adding the class module that contains the error-logging code to your project. For more information about creating custom objects, see Chapter 9, "Custom Classes and Objects."
The sample code in the modErrorCode module in ErrorHandlers.dot in the ODETools\V9\Samples\OPG\Samples\CH08 subfolder on the Office 2000 Developer CD-ROM illustrates several techniques for displaying and logging errors.
The ADOErrorExample1 procedure contains several lines of code that generate errors. When an error occurs in this procedure, the error handler calls the HandleErrorMessage procedure and passes the ADO Errors collection, the VBA Err object, and an integer specifying how the error information is to be recorded. The procedure can record errors by using a message box, the Debug window, a text file, or a database.
The HandleErrorMessage procedure is not necessarily the best way to handle error logging in your own applications, but rather is presented to illustrate the variety of techniques you can use.
One of the most interesting aspects of the HandleErrorMessage procedure is how it handles multiple errors passed to it in the ADO Errors collection. The techniques illustrated in the HandleErrorMessage procedure show how to retrieve all the error information from all the Error objects in the collection as well as information contained in the VBA Err object for the same error.