Simple Microsoft Access MouseOver and MouseOut Custom Events
Microsoft Access supports mouse events for a number of control types. The mouse events Access supports are MouseDown, MouseUp, and MouseMove. When the mouse is moved over a control the MouseMove event is fired, not just once, but potentially for every pixel, depending on the speed of the mouse movement. There is not a specific event for when the mouse moves out from the control, there is just a silencing of the MouseMove events. Once the mouse moves away from a control there are trappable MouseMove events for the underlying object, such as the form itself. Actually you need to work with the Form Section as the Form_MouseMove only fires when you first move onto the form If there are overlapped controls or container objects such as a TabControl then the form (or more particularly the form section) MouseMove will not fire, until the mouse is moved over an unpopulated part of the section. The example does not try to catch these more complex examples.
You could write the code to detect this mouse over and mouse out situations on the form however it is a lot easier to write this as a class, and much easier to use in the form module
[sourcecode language=”VB” gutter=”false”] ‘—————————————————————-‘ Module : clsSimpleMouseOver
‘ Author : Brileigh Computer Services (c) 2012
‘ Purpose : Create a simple MouseOver and MouseOut trappable
‘ event
‘ This particular example demonstrates the
‘ principle and is only applicable as it stands
‘ for a CommandButton placed on the Detail Section
‘ of a Form
‘—————————————————————-
Option Compare Database
Option Explicit
Private m_objForm As Access.Form
Private WithEvents m_objDetail As Access.Section
‘ In this example the target is a CommandButton
‘ but can be any Control that supports MouseMove events
Private WithEvents m_ctlControl As Access.CommandButton
Public Event MouseOut()
Public Event MouseOver()
Private m_boolLastMouseIsOver As Boolean
Private Const EVENTED As String = "[Event Procedure]"
Private Sub Class_Terminate()
Set m_objForm = Nothing
Set m_ctlControl = Nothing
End Sub
Public Property Set Form(objForm As Access.Form)
Set m_objForm = objForm
‘ Setup the MouseMove for the surrounding object to the
‘TargetControl, in this case the Detail Section but
‘could be an overlapped control, a tab page, or a footer
Set m_objDetail = objForm.Section("Detail")
m_objDetail.OnMouseMove = EVENTED
End Property
Public Property Set TargetControl(ctl As Access.CommandButton)
Set m_ctlControl = ctl
m_ctlControl.OnMouseMove = EVENTED
End Property
Private Sub m_ctlControl_MouseMove(Button As Integer, _
Shift As Integer, _
X As Single, _
Y As Single)
‘ So the event is fired once only until a
‘ MouseOut occurs
If Not m_boolLastMouseIsOver Then
m_boolLastMouseIsOver = True
RaiseEvent MouseOver
End If
End Sub
Private Sub m_objDetail_MouseMove(Button As Integer, _
Shift As Integer, _
X As Single, _
Y As Single)
‘ So the event is fired once only until a
‘ MouseOver occurs
If m_boolLastMouseIsOver Then
m_boolLastMouseIsOver = False
RaiseEvent MouseOut
End If
End Sub
The Form’s module with say two CommandButtons might look like:
[sourcecode language=”VB” gutter=”false”] ‘———————————————————‘ Module : Form_Form1
‘ Author : Brileigh Computer Services (c) 2012
‘ Purpose : Module to demonstrate MouseOver events for
‘ multiple command buttons
‘———————————————————
Option Compare Database
Option Explicit
‘ Subscribe to the Custom Events for two CommandButtons
Private WithEvents obj1 As clsSimpleMouseOver
Private WithEvents obj2 As clsSimpleMouseOver
Private m_caption1 As String ‘ original captions
Private m_caption2 As String
Private Sub Form_Close()
Set obj1 = Nothing
Set obj2 = Nothing
End Sub
Private Sub Form_Load()
‘ Create instances of a clsSimpleMouseOver object
‘ for each required CommandButton
Set obj1 = New clsSimpleMouseOver
With obj1
‘ to set the region that will trigger MouseOut
Set .Form = Me
Set .TargetControl = Me.Command1
End With
Set obj2 = New clsSimpleMouseOver
With obj2
Set .Form = Me
Set .TargetControl = Me.Command2
End With
End Sub
‘—————————————————–
‘ Procedure : obj1_MouseOver
‘ Purpose : To handle the custom MouseOver Event
‘ for a control
‘—————————————————–
Private Sub obj1_MouseOver()
‘ save original captions
m_caption1 = Me.Command1.Caption
‘ set new caption
Me.Command1.Caption = "Click me"
‘…… or whatever code is required
End Sub
Private Sub obj1_MouseOut()
‘ restore original caption
Me.Command1.Caption = m_caption1
End Sub
Private Sub obj2_MouseOver()
m_caption2 = Me.Command2.Caption
Me.Command2.Caption = "Click me 2"
End Sub
Private Sub obj2_MouseOut()
Me.Command2.Caption = m_caption2
End Sub
‘
[/sourcecode]
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 also respond to the MouseMove event in the contiguous control, as well as in the Form Section.
Comments closed.