Canceling Events

Under some circumstances, you may want to include code in an event procedure that cancels the associated event. For example, you may want to include code that cancels the Open event in an Open event procedure for a form, preventing the form from opening if certain conditions are not met.

You can cancel the following events:

BeforeDelConfirm

Format

BeforeInsert

BeforeUpdate

DblClick

Open

Delete

Print

Unload

Exit

Filter

ApplyFilter

NoData

KeyPress (only with a macro)

MouseDown (only the right mouse button, and only with a macro)

You cancel an event by specifying a macro containing the CancelEvent action as the corresponding event property setting or, with the exception of the MouseDown and KeyPress events, by setting an event procedure’s Cancel argument to True. For example, to prevent a form from opening, you can:

See Also   For more information on the CancelEvent action, search the Help index for “CancelEvent action.”

You can see an example of canceling an Unload event in the EventHistory form of the Orders sample application. Because the EventHistory form is required by the ShowEvents form, it’s important to close the ShowEvents form before closing the EventHistory form. To ensure that the ShowEvents form isn’t left open without the EventHistory form, the EventHistory form’s Unload event procedure cancels the Unload event if you try to close it when the ShowEvents form is open.

Private Sub Form_Unload (Cancel As Integer)
' Reminds you to close the ShowEvents form, ensuring that
' it isn't left open without an open EventHistory form.

	Dim intX As Integer

	' Loop through the open forms. If the ShowEvents form is found,
	' display a message and cancel the Unload event.
	For intX = 0 To Forms.Count - 1
		If Forms(intX).Name = "ShowEvents" Then
			MsgBox "Please close the ShowEvents form."
			Cancel = True
			Exit For
		End If
	Next intX
End Sub