Solved Get the Control that is executing the function (1 Viewer)

Saphirah

Active member
Local time
Today, 22:37
Joined
Apr 5, 2020
Messages
163
Hello everyone,

i want a control to receive focus whenever the mouse hovers above it.
For this i use the following VBA Code:

Code:
Private Sub ExampleCtrl_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    ExampleCtrl.SetFocus
End Sub

Now this should happen on multiple controls. Ofc it is very hideous to program this for every control, because it is always the same code.
As you probably know, you can use functions in the Controls Mouse Move Events. (Screenshot is german, but you get the point)
1620813554815.png

But inside this function i have no possibility to determine which control should receive the focus, because i do not know which control executed the function.
Sooooo.... How can one do that? Is there maybe a better way to do that?

Thank you very much for your help!
 
Last edited:

Gasman

Enthusiastic Amateur
Local time
Today, 21:37
Joined
Sep 21, 2011
Messages
14,238
Why would you want to set focus on a report control? :unsure:

If it was a Form, then pass the Form and Control Objects and use them in your function?
 

Saphirah

Active member
Local time
Today, 22:37
Joined
Apr 5, 2020
Messages
163
Why would you want to set focus on a report control? :unsure:

If it was a Form, then pass the Form and Control Objects and use them in your function?
I do not want to set the focus on a report control, but a form control :) My bad.
How do i pass the Form and Control Object? Like this?
1620814328358.png
 

cheekybuddha

AWF VIP
Local time
Today, 21:37
Joined
Jul 21, 2014
Messages
2,272
Firstly, the MouseMove event can fire continuously when it is activated so you want to make a test before performing any action, otherwise you may find you get a lot of flicker.

So you can code your function a bit more defensively:
Code:
Function ReceiveFocusOnMouse(ctl As Control) As Boolean

  If Not Screen.ActiveControl Is ctl Then
    ctl.SetFocus
    ReceiveFocusOnMouse = True
  End If

End Function

Obviously, you now are faced with the problem of writing the control name into the function in the control's MouseMove property.

One way to deal with this is in code. Create a function:
Code:
Function SetCtlsMouseFocus(frm As Form, ParamArray ctlNames As Variant) As Boolean

  Dim i As Integer

  If UBound(ctlNames) >= 0 Then
    For i = 0 To UBound(ctlNames)
      frm(ctlNames(i)).OnMouseMove = "=ReceiveFocusOnMouse([" & ctlNames(i) & "])"
    Next i
    SetCtlMouseFocus = Err = 0
  End If
 
 End Function

Then, in your your form's Load event you can use:
Code:
Private Sub Form_Load()

' List the names of all the controls you want to receive focus
' after the first argument which is Me (the current form)
  Call SetCtlsMouseFocus(Me, "txtField1", "txtField2", "lstSomeListbox")

End Sub

(NB all aircode)

hth,

d
 

Gasman

Enthusiastic Amateur
Local time
Today, 21:37
Joined
Sep 21, 2011
Messages
14,238
I would have a function in the form that you call from that event.
In the function called, you can pass the form and control object to your function that resides in a module.

Theory only :(

Edit: I am sure some experts will say you can create the event code when the form loads as well?
Something I have never done though.
 

Saphirah

Active member
Local time
Today, 22:37
Joined
Apr 5, 2020
Messages
163
Thank you very much everyone! And thank you cheekybuddha for the awesome example code!
This solves my problem :)
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 16:37
Joined
Feb 19, 2002
Messages
43,233
I'm glad you're happy. However, changing how a form works will ultimately confuse people because every other form they interact with in other applications will work differently. Consistency is very important when developing a user interface.
 

Users who are viewing this thread

Top Bottom