Click Event -- Event Procedures

Description

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

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


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.)

For a command button only, Microsoft Access runs the event procedure when the user chooses the command button by pressing the ENTER key or an access key. The event procedure is run once. If you want the event procedure to be run repeatedly while the command button is pressed, set its AutoRepeat property to Yes. For other types of controls, you must click the control using the mouse button to trigger the Click event.

You can’t cancel the Click event.

See Also

Click Event — Macros.

Example

In this 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 following example, add the code to the Declarations section of a form named Sales that contains a check box called ReadOnly and a text box named Amount.


Private Sub ReadOnly_Click ()
    Dim ctl As Control
    Set ctl = Forms!Sales!Amount
    If Forms!Sales!ReadOnly = True Then    ' If checked.
        ctl.Enabled = False    ' Disable editing.
        ctl.Locked = True 
    Else        ' If cleared.
        ctl.Enabled = True    ' Enable editing.
        ctl.Locked = False
    End IfSub