Handling Database Errors

Database errors or "engine errors" occur when errors occur at run time in record-level event code. For example, a database error occurs when a user attempts to store a null value in a field that does not accept null values.

Typically, when a database error occurs, the underlying database engine that detects the error generates an error message. However, the exact nature of the error message depends on the database that is accessed. For example, error messages generated by a remote database server might be different than those generated by a local Visual FoxPro database table. In addition, engine-level errors can be generic because the database engine has no information about the context for updating a record. As a result, error messages generated by a database engine are not as useful to a Visual FoxPro application user.

To handle database errors in a more application-specific way, you can create triggers. A trigger is an expression that is bound to a table and is invoked when any of the table's records are modified using a one of the specified data manipulating commands. You can write trigger code in the form of user-defined functions or stored procedures using the CREATE TRIGGER command to trap application-specific error conditions and generate appropriate messages.


If you handle database errors using triggers, two error messages can occur: one from your trigger and another from the underlying database engine. You can avoid this possibility by turning buffering on. Buffering makes it possible to delay record updates to the underlying database but to still call your trigger when the record is updated. You can turn on buffering and display custom error messages using the CURSORSETPROP( ) function. For more information, see CURSORSETPROP( ) Function.

For more information, see CREATE TRIGGER Command and How to: Create and Manage Stored Procedures.

See Also

© , 1996-2020 • Updated: 11/10/20
Comment or report problem with topic