Database Programming Techniques > Handling Database Errors > Processing trapped errors |
After trapping a reported error, the task remains to process the error appropriately within the context of your program. This requires analyzing the error code and error text to determine the problem and, of course, deciding what to do about it.
In many cases, you will find that simply reporting the error to the user is the most appropriate. When an error is trapped through an error event, reporting the error can be handled automatically by not coding the error event at all. By default, a message box is displayed indicating the error. Additional details of generalized Error events can be obtained from the Visual Basic help file.
Trappable errors, which originate from Visual Basic code, will result in program termination unless the On Error statement is used to set up an error handler. Appropriate messages for the errors can usually be obtained from the Visual Basic Error function, but sometimes require inspection of the object in which the original error occurred. This will be discussed later.
When it becomes necessary to do more than display an error message, the second key point (the source of the actual error) must be considered. In the example presented earlier, where non-numeric data is placed in a numeric field, the error "Data type conversion error" is usually issued. This error does not originate from the grid, or even the Data control. It comes from the database engine—DAO in the case of an Access database.
Database errors usually result in a cascade of errors, each a direct result of the other. In our example, the first error encountered is the "Data conversion error" generated by the database engine when attempting to update the numeric field. As a result, a general failure error occurs from the row update. The row update error causes the row change operation to fail, also with a general error code. This cascade of errors makes the problem difficult to analyze, since only the general error of the row change failure is reported to the grid from the data control, and finally passed on to your Visual Basic program as the trappable error code or the DataError argument of the grid's Error event. In order to discern the true nature of the problem, it is necessary to inspect the source of the original error—the Errors collection of the database engine. This collection object is discussed fully in the Visual Basic help file.
To simplify handling of errors in the grid's Error event, True DBGrid supports an ErrorText property. This property is read-only, and is available only during the execution of the Error event. It returns the error message string associated with the underlying data source error that ultimately caused the grid's Error event to fire.