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.
Setting
The CancelEvent action doesn't have any arguments.
Remarks
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 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 | Dirty | MouseDown |
BeforeDelConfirm | Exit | NoData |
BeforeInsert | Filter | Open |
BeforeUpdate | Format | |
DblClick | KeyPress | Unload |
Delete |
Note You can use the CancelEvent action with the MouseDown event only to cancel the event that occurs when you right-click an object.
If a control's OnDblClick event property setting specifies a macro containing the CancelEvent action, the action cancels the DblClick 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 to be carried out or open the macro and delete the CancelEvent action. 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.