Change Event — Event Procedures

Description

To create an event procedure that runs 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 has the following argument.

Argument

Description

controlname

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


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 (for example, by setting the control's Text property). 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 the example, add the following event procedure to a form named Orders that contains a text box named 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 more than three, reset value of text box.
    If intLetter < 3 Then
        intLetter = intLetter + 1
    Else
        MsgBox "Only three characters are allowed."
        intLetter = 0
        Forms!Orders!ShipRegion.Undo
    End If
End Sub