BeforeUpdate, AfterUpdate Events -- Event Procedures

Description

To create an event procedure that is run 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 uses the following argument.

Argument Description
controlname A string that is the name of the control affected by the BeforeUpdate event procedure.
Cancel The setting determines if the BeforeUpdate event occurs. Setting Cancel to True (-1) cancels the BeforeUpdate event


The AfterUpdate event procedure uses the following argument.

Argument Description
controlname A string that is the name of the control affected by the AfterUpdate event procedure.


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

This example shows how you can use a BeforeUpdate event procedure to check whether a product name has already been entered. 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 in the database.

To try the following example, add the following code to the Declarations section of 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] ='" & Forms!Products!ProductName & "'"))) Then
        MsgBox "Product has already been entered in the database."
        Cancel = True
        Me!ProductName.Undo
    End IfSub