Validate Event
Object List Next Object
Defined By: Recordset
Description:
The Validate event is triggered right before a different record becomes the
current record. It is typically used to initiate an action that will verify user
input before it is accepted.
Usage:
Sub Validate (state As Long, save As Long)
The action that actually triggers the Validate event is indicated by the
following State settings:
State Description
0 Cancel the operation when the Sub exits.
- MoveFirst method.
- MovePrevious method.
- MoveNext Method.
- MoveLast method
- AddNew method.
- Update operation (not Update Record).
- Delete method.
- Find method
10 The Close method.
11 The form is being unloaded
Details:
The purpose of the Validate event is to initiate an action before a different
record becomes current. This event typically evaluates data values to make sure
they're in the correct format and are in an acceptable range of responses. The
Validate event can stop the changes to the database and the movement from the
current record if any values are not acceptable. For example, you can set up
conditions that test whether the input falls within acceptable ranges, whether
data is in the correct format, or even if a particular field is allowed to
change.
The State argument indicates what action triggered the Validate event. You may
want to take different actions depending on what triggered it. For example,
you may want to turn off validation routines if the Delete action triggered it,
then follow up the deletion by deleting all records on the "many" side of a
one-to-many relationship.
You can set the State argument to some other action that will be performed
after the event handler is completed. If you do not change it, the action that
triggered the event initially will be completed. If a Move method or AddNew was
triggering the event, you can change it to some other Move method or AddNew; they
are interchangeable. You can't, however, change an AddNew or a Move method
into any other action or a program exception is thrown, if it is not simply
ignored. You can always stop any action by setting State to 0. In this case the
current record remains where it is.
At first, the Save argument indicates whether bound data has changed. If any
bound controls have changed, Save is set to True. You can then set a value for
Save at the end of the event procedure if you would like to do things such as
update or save data or change values.
No other methods can be used on the underlying database while a Validate event
is executing. In the following example, data from two textboxes (TextBox1 and
TextBox2) are examined before the record may be moved.
Sub RecordSet_Validate(state as Long, save as Long)
If TextBox1.DataChanged = True Then 'Don't change ID
If RecordSet.EditMode <> "AddNew" Then 'unless new record
TextBox1.DataChanged = False 'Discard changes
End If
If (Val(TextBox2.Text) > 10 Then 'Greater than 10
InfoBox.Message("", "This value must be less than 10.")
state = 0 'Cancel move
save = 0 'Don't save anything
End Sub
See Also:
AddNew method; Delete method; EditMode property; FindFirst, FindLast, FindNext, FindPrev methodsMoveFirst, MoveLast, MoveNext, MovePrev methods; Update, UpdateAll methods