Share via


Click Event [Access 2003 VBA Language Reference]

The Click event occurs when the user presses and then releases a mouse button over an object.

Private Sub object_Change()

Object   The name of a Form or control.

Remarks

  • The Click event applies only to forms, form sections, and controls on a form, not controls on a report.
  • This event doesn't apply to check boxes, option buttons, or toggle buttons in an option group. It applies only to the option group itself.
  • This event doesn't apply to a label attached to another control, such as the label for a text box. It applies only to "freestanding" labels. Clicking an attached label has the same effect as clicking the associated control. The normal events for the control occur, not any events for the attached label.
  • This event applies to a control containing a hyperlink.

To run a macro or event procedure when this event occurs, set the OnClick property to the name of the macro or to [Event Procedure].

On a form, this event occurs when the user clicks a blank area or record selector on the form.

For a control, this event occurs when the user:

  • Clicks a control with the left mouse button. Clicking a control with the right or middle mouse button does not trigger this event.

  • Clicks a control containing hyperlink data with the left mouse button. Clicking a control with the right or middle mouse button does not trigger this event. When the user moves the mouse pointer over a control containing hyperlink data, the mouse pointer changes to a "hand" icon. When the user clicks the mouse button, the hyperlink is activated, and then the Click event occurs.

  • Selects an item in a combo box or list box, either by pressing the arrow keys and then pressing the ENTER key or by clicking the mouse button.

  • Presses SPACEBAR when a command button, check box, option button, or toggle button has the focus.

  • Presses the ENTER key on a form that has a command button whose Default property is set to Yes.

  • Presses the ESC key on a form that has a command button whose Cancel property is set to Yes.

  • Presses a control's access key. For example, if a command button's Caption property is set to &Go, pressing ALT+G triggers the event.

Typically, you attach a Click event procedure or macro to a command button to carry out commands and command-like actions. For the other applicable controls, use this event to trigger actions in response to one of the occurrences discussed earlier in this topic.

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

The Click event for a command button occurs when you choose the command button. In addition, if the command button doesn't already have the focus when you choose it, the Enter and GotFocus events for the command button occur before the Click event.

Double-clicking a control causes both the DblClick and Click events to occur. For command buttons, double-clicking triggers the following events, in this order:

MouseDown → MouseUp → Click → DblClick → Click

You can use a CancelEvent action in a DblClick macro to cancel the second Click event. For more information, see the DblClick event topic.

The Click event for an option group occurs after you change the value of one of the controls in the option group by clicking the control. For example, if you click a toggle button, option button, or check box in an option group, the Click event for the option group occurs after the BeforeUpdate and AfterUpdate events for the option group.

Tip

MouseDownMouseUp

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

Applies to | BoundObjectFrame Object | CheckBox Object | ComboBox Object | CommandButton Object | Form Object | Image Object | Label Object | ListBox Object | ObjectFrame Object | OptionButton Object | OptionGroup Object | Page Object | Rectangle Object | Section Object | TabControl Collection | TextBox Object | ToggleButton Object

See Also | AutoRepeat Property | Cancel Property | Caption Property | DblClick Event | Default Property | OnClick Property | OnPush Property