MouseMove Event

Microsoft Access Visual Basic

Private Sub object_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)

Object    The name of a Form, Form section, or control on a Form.

Button    The button that was pressed (MouseDown) or released (MouseUp) to trigger the event. If you need to test for the Button argument, you can use one of the following intrinsic constants as bit masks:

Constant Description

acLeftButton The bit mask for the left mouse button.

acRightButton The bit mask for the right mouse button.

acMiddleButton The bit mask for the middle mouse button.

Shift    The state of the SHIFT, CTRL, and ALT keys when the button specified by the Button argument was pressed or released. If you need to test for the Shift argument, you can use one of the following intrinsic constants as bit masks:

Constant Description

acShiftMask The bit mask for the SHIFT key.

acCtrlMask The bit mask for the CTRL key.

acAltMask The bit mask for the ALT key.

X, Y    The x and y coordinates for the current location of the mouse pointer. The X and Y arguments are always expressed in twips.

Remarks

The MouseMove event applies only to forms, form sections, and controls on a form, not controls on a report.

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. Moving the mouse pointer over an attached label has the same effect as moving the pointer over the associated control. The normal events for the control occur; no separate events occur for the attached label.

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

The MouseMove event is generated continually as the mouse pointer moves over objects. Unless another object generates a mouse event, an object recognizes a MouseMove event whenever the mouse pointer is positioned within its borders.

To cause a MouseMove event for a form to occur, move the mouse pointer over a blank area, record selector, or scroll bar on the form. To cause a MouseMove event for a form section to occur, move the mouse pointer over a blank area of the form section.

Notes

  • Moving a form can trigger a MouseMove event even if the mouse is stationary. MouseMove events are generated when the form moves underneath the pointer. If a macro or event procedure moves a form in response to a MouseMove event, the event can cascade (that is, continually generate MouseMove events).

  • If two controls are very close together, and you move the mouse pointer quickly over the space between them, the MouseMove event may not occur for the space (for example, this might be the MouseMove event for the form section). In such cases, you may need to respond to the MouseMove event in the contiguous control, as well as in the form section.

To run a macro or event procedure in response to pressing and releasing the mouse buttons, you use the MouseDown and MouseUp events.

Macro

You can use a MouseMove macro to respond whenever the user moves the mouse over a form or control. However, macros can't return the button code and determine the state of the mouse buttons or determine the coordinates of the mouse pointer when the movement occurs, so you typically use event procedures with this event.

You can't use the CancelEvent action in a MouseMove macro.

Example

The following example determines where the mouse is and whether the left mouse button and/or the SHIFT key is pressed. The x and y coordinates of the mouse pointer position are displayed in a label control as you move the mouse.

To try the example, add the following event procedure to a form that contains a label named Coordinates:

Private Sub Detail_MouseMove(Button As Integer, _
     Shift As Integer, X As Single, Y As Single)
    Dim intShiftDown As Integer, intLeftButton As Integer

    Me!Coordinates.Caption = X & ", " & Y
    ' Use bit masks to determine state of
    ' SHIFT key and left button.
    intShiftDown = Shift And acShiftMask
    intLeftButton = Button And acLeftButton
    ' Check that SHIFT key and left button 
    ' are both pressed.
    If intShiftDown And intLeftButton > 0 Then
        MsgBox "Shift key and left mouse button were pressed."
    End If
End Sub