Change Event -- Event Procedures

Description

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

Syntax

Private Sub controlname_Change( )

The Change event procedure uses the following argument.

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


Remarks

You can’t cancel the Change event.

To prevent a cascading event, avoid creating a Change event procedure for a control that alters the control’s contents. If you do create such an event procedure, be sure to set a variable that prevents further changes while the current change is in progress.

See Also

Change Event — Macros.

Example

The following example counts the number of characters entered in a text box. If more than three characters are entered, a message is displayed and the count is reset to 0.

To try this example, add the following code to the Declarations section of a module for a form named Orders that contains a text box called ShipRegion.


Private Sub ShipRegion_Change()
    ' Declare variable as static to preserve value between calls.
    Static intLetter As Integer

    ' Check to see how many letters have been entered.
    If intLetter < 3 Then
        intLetter = intLetter + 1
    ' If more than three, reset value of text box.
    Else
        MsgBox "Only three characters are allowed."
        intLetter = 0
        Forms!Orders!ShipRegion.Undo
    End IfSub