You have seen in the first, example of this chapter that the Data control cap also be used for editing records. Database applications, however, should have the means to validate user input before attempting to save data to the database. The Data control provides a few events-and methods that let you validate the input before committing it to the database.
The Validate Event
The validation of input takes place in the Data control’s Validate event, whose declaration is as follows:
Private Sub object_Validate (action As Integer; save As Integer)
The action entry is an integer indicating the operation that caused this event to occur, and it can have one of the values shown in Table 17.7.
The value vbDataActionCancel can be set from within code to cancel the operation that triggered the Validate event. For example, if the user changes the data on a data-bound control and then clicks the Move Next button of the control, you can reject the changes from within your code and cancel the Move Next operation.
The save argument is a Boolean expression specifying whether bound data has changed. You can set this argument to False to reject the changes.
The Error Event
Another useful event in data validation operations is the Error event. When you update records in a database, the JET engine ensures that the data entered don’t invalidate the rules incorporated in the design of the database, For example, if an index requires that a specific field be unique, it won’t update the record if the value entered has already been used. If a field exceeds its maximum length, the changes will also be rejected. In other words, the JET engine performs some data validation 0 its own. It also performs any validation steps you specified in the Validation Rule field, in each table’s design.
If the data violates rules specified in the design of the database, a runtime error is generated. Before the error message is displayed, the Error event is generated. You can find the condition that generated the error and handle it from within your code.
The declaration of the Error event is as fellows:
. Sub Error (dataerr As Integer, response As Integer)
The dataerr entry is the error number, and response is an integer that you can see from within the event to specify how the error will be handled. The response argument can take one of the values shown in Table 17.8. You must handle the error from within the Error event and then set the response argument to 0 to prevent the runtime error.