BeforeInsert, AfterInsert Events -- Event Procedures

Description

To create an event procedure that is executed when the BeforeInsert or AfterInsert event occurs, set the BeforeInsert or AfterInsert property to [Event Procedure], and click the Build button.

Syntax

Private Sub Form_BeforeInsert (Cancel As Integer)Private Sub Form_AfterInsert ( )

The BeforeInsert event procedure uses the following argument.

Argument Description
Cancel The setting determines if the BeforeInsert event occurs. Setting Cancel to True (-1) cancels the BeforeInsert event.


Remarks

You can’t cancel the AfterInsert event.

See Also

BeforeInsert, AfterInsert Events — Macros.

Example

This example shows how you can use a BeforeInsert event procedure to verify that the user wants to create a new record, and an AfterInsert event procedure to requery the record source for the Employees form after a record has been added.

To try this example, add the following code to the Declarations section of a form named Employees that is based on a table or query. Switch to the form’s Datasheet view and try to insert a record.


Private Sub Form_BeforeInsert (Cancel As Integer)
    If MsgBox("Insert new record here?", vbOKCancel) = vbCancel Then
        Cancel = True
    End IfSub
Sub Form_AfterInsert ()
    Forms!Employees.Requery    Sub