Click Event — Event Procedures

Description

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

Syntax

Private Sub Form_Click( )

Private Sub controlname_Click( )

The Click event procedure has the following argument.

Argument

Description

controlname

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


Remarks   When the user double-clicks a list box, the following events occur, in this order:

MouseDown MouseUp Click DblClick

Other controls have a similar order of events. When you're creating event procedures for these related events, be sure that their event procedure code doesn't conflict. (For example, the DblClick event procedure shouldn't contain code that cancels actions carried out in the Click event procedure.)

You can't cancel the Click event.

See Also   Click event — macros.

Example

In the following example, the Click event procedure is attached to the ReadOnly check box. The event procedure sets the Enabled and Locked properties of another control on the form, the Amount text box. When the check box is clicked, the event procedure checks whether the check box is being selected or cleared and then sets the text box's properties to enable or disable editing accordingly.

To try the example, add the following event procedure to a form that contains a check box called ReadOnly and a text box named Amount.

Private Sub ReadOnly_Click()
    With Me!Amount
        If Me!ReadOnly = True Then        ' If checked.
            .Enabled = False                ' Disable editing.
            .Locked = True
        Else                                    ' If cleared.
            .Enabled = True                    ' Enable editing.
            .Locked = False
        End If
    End With
End Sub