You can use the CancelEvent action to cancel the event that caused Microsoft Access to run the macro containing this action. The macro name is the setting of an event property such as BeforeUpdate, OnOpen, OnUnload, or OnPrint.
The CancelEvent action doesn’t use any arguments.
In a form, you typically use the CancelEvent action in a validation macro with the BeforeUpdate event property. When a user enters data in a control or record, Microsoft Access runs the macro before adding the data to the database. If the data fails the validation conditions in the macro, the CancelEvent action cancels the update process before it starts.
Often, you can use this action with the MsgBox action to indicate that the data has failed the validation conditions and to provide helpful information about the kind of data that should be entered.
The following events can be canceled by the CancelEvent action:
ApplyFilter |
Delete |
MouseDown |
BeforeDelConfirm |
Exit |
NoData |
BeforeInsert |
Filter |
Open |
BeforeUpdate |
Format |
|
DblClick |
KeyPress |
Unload |
Note You can use the CancelEvent action with the MouseDown event only to cancel the event that occurs when you click the right mouse button.
If a control’s OnDblClick event property setting specifies a macro containing the CancelEvent action, the action cancels the double-click event.
For events that can be canceled, the default behavior for the event (that is, what Microsoft Access typically does when the event occurs) occurs after the macro for the event runs. This enables you to cancel the default behavior. For example, when you double-click a word that the insertion point is on in a text box, Microsoft Access normally selects the word. You can cancel this default behavior in the macro for the DblClick event and perform some other action, such as opening a form containing information about the data in the text box. For events that can’t be canceled, the default behavior occurs before the macro runs.
Note If a form’s OnUnload event property specifies a macro that carries out a CancelEvent action, you won’t be able to close the form. You must either correct the condition that caused the CancelEvent action or open the macro and delete the CancelEvent action to close the form. If the form is a modal form, you won’t be able to open the macro.
To carry out the CancelEvent action in Visual Basic, use the CancelEvent method of the DoCmd object.
CancelEvent Method, StopMacro Action.