BeforeUpdate, AfterUpdate Events — Event Procedures

Description

To create an event procedure that runs when the BeforeUpdate or AfterUpdate event occurs, set the BeforeUpdate or AfterUpdate property to [Event Procedure], and click the Build button.

Syntax

Private Sub Form_BeforeUpdate(Cancel As Integer)

Private Sub controlname_BeforeUpdate(Cancel As Integer)

Private Sub Form_AfterUpdate( )

Private Sub controlname_AfterUpdate( )

The BeforeUpdate event procedure has the following arguments.

Argument

Description

controlname

The name of the control whose BeforeUpdate event procedure you want to run.

Cancel

The setting determines if the BeforeUpdate event occurs. Setting the Cancel argument to True (–1) cancels the BeforeUpdate event.


The AfterUpdate event procedure has the following argument.

Argument

Description

controlname

The name of the control whose AfterUpdate event procedure you want to run.


Remarks

You can't cancel the AfterUpdate event.

You can use an AfterUpdate event procedure to display a different page in the form or move the focus to a particular control or record. For example, when the user enters a value in a Category control on a form, you can use the GoToPage method in the AfterUpdate event procedure for the form to move the focus to the page of the form that contains the controls for that category.

See Also

BeforeUpdate, AfterUpdate events — macros.

Example

The following example shows how you can use a BeforeUpdate event procedure to check whether a product name has already been entered in the database. After the user types a product name in the ProductName box, the value is compared to the ProductName field in the Products table. If there is a matching value in the Products table, a message is displayed that informs the user that the product has already been entered.

To try the example, add the following event procedure to a form named Products that contains a text box called ProductName.

Private Sub ProductName_BeforeUpdate(Cancel As Integer)
    If(Not IsNull(DLookup("[ProductName]", "Products", _
            "[ProductName] ='" & Me!ProductName & "'"))) Then
        MsgBox "Product has already been entered in the database."
        Cancel = True
        Me!ProductName.Undo
    End If
End Sub