find the name of the control that triggered an event

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Is there a way to get name of the control that triggered a MouseMove event,
without explicitly passing the name?

The solutions recommended in Subject: Get name of a control, 5/16/2006 1:40
PM PST By: ScottH--- ,

to use Screen.ActiveControl or Me.ActiveControl won't work, the MouseMove
control isn't necessarily the active control.

Thanks!
 
Unfortunately, Access/VBA provides no way to know the name of the control
whose event is triggered, or the currently executing procedure.

If you have a bunch of controls you need to process, and you don't need the
arguments (X, Y, etc.), you could programmatically assign the OnMouseMove
property of each control so it calls a generic function and passes the
control name. Assuming Form1 is open in design view, and assuming you want
to do this for all the text boxes on the form:

For Each ctl In Forms!Form1.Controls
If ctl.ControlType = acTextBox Then
ctl.OnMouseMove = "=MyFunc([" & ctl.Name & "])"
End If
Next

Function MyFunc(strControlName As String)
 
Allen, your reply definitely helps--thanks. Maybe the control can pass its
own name? Something like:

Private Sub txtMyTextBox_MouseMove()
Dim strControlName As String ', ctl As Control???
strControlName = ctl.Name ' ???
InstructionLabelTexts strControlName 'Passes the name.
End Sub

Then I can test to see which cortrol is calling. I'm not getting in your
example how ctl.Name gets txtMyTextBox's name.

I've also played with:

???.Properties("Name").Value

trying to pass the name.

Allen Browne said:
Unfortunately, Access/VBA provides no way to know the name of the control
whose event is triggered, or the currently executing procedure.

If you have a bunch of controls you need to process, and you don't need the
arguments (X, Y, etc.), you could programmatically assign the OnMouseMove
property of each control so it calls a generic function and passes the
control name. Assuming Form1 is open in design view, and assuming you want
to do this for all the text boxes on the form:

For Each ctl In Forms!Form1.Controls
If ctl.ControlType = acTextBox Then
ctl.OnMouseMove = "=MyFunc([" & ctl.Name & "])"
End If
Next

Function MyFunc(strControlName As String)

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

donaldg said:
Is there a way to get name of the control that triggered a MouseMove
event,
without explicitly passing the name?

The solutions recommended in Subject: Get name of a control, 5/16/2006
1:40
PM PST By: ScottH--- ,

to use Screen.ActiveControl or Me.ActiveControl won't work, the MouseMove
control isn't necessarily the active control.

Thanks!
 
Allen, when I tested your recommendation as below, strControlName returns the
field contents rather than the control's name ie, instead of "txtTitle" it
returns "Once and Future King."

Sub PassName()
For Each ctl In Forms!MyBookList.Controls
If ctl.ControlType = acTextBox Then
ctl.OnMouseMove = "=MyFunc([" & ctl.Name & "])"
End If
Next
End Sub

Public Function MyFunc(strControlName As String)
Debug.Print strControlName
End Function

Allen Browne said:
Unfortunately, Access/VBA provides no way to know the name of the control
whose event is triggered, or the currently executing procedure.

If you have a bunch of controls you need to process, and you don't need the
arguments (X, Y, etc.), you could programmatically assign the OnMouseMove
property of each control so it calls a generic function and passes the
control name. Assuming Form1 is open in design view, and assuming you want
to do this for all the text boxes on the form:

For Each ctl In Forms!Form1.Controls
If ctl.ControlType = acTextBox Then
ctl.OnMouseMove = "=MyFunc([" & ctl.Name & "])"
End If
Next

Function MyFunc(strControlName As String)

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

donaldg said:
Is there a way to get name of the control that triggered a MouseMove
event,
without explicitly passing the name?

The solutions recommended in Subject: Get name of a control, 5/16/2006
1:40
PM PST By: ScottH--- ,

to use Screen.ActiveControl or Me.ActiveControl won't work, the MouseMove
control isn't necessarily the active control.

Thanks!
 
You are right.

Change the middle line of PassName() to:
ctl.OnMouseMove = "=MyFunc(""" & ctl.Name & """)"

Or, change MyFunc() to accept the control and give its name:
Public Function MyFunc(ctl As Control)
Debug.Print ctl.Name
End Function

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

donaldg said:
Allen, when I tested your recommendation as below, strControlName returns
the
field contents rather than the control's name ie, instead of "txtTitle" it
returns "Once and Future King."

Sub PassName()
For Each ctl In Forms!MyBookList.Controls
If ctl.ControlType = acTextBox Then
ctl.OnMouseMove = "=MyFunc([" & ctl.Name & "])"
End If
Next
End Sub

Public Function MyFunc(strControlName As String)
Debug.Print strControlName
End Function

Allen Browne said:
Unfortunately, Access/VBA provides no way to know the name of the control
whose event is triggered, or the currently executing procedure.

If you have a bunch of controls you need to process, and you don't need
the
arguments (X, Y, etc.), you could programmatically assign the OnMouseMove
property of each control so it calls a generic function and passes the
control name. Assuming Form1 is open in design view, and assuming you
want
to do this for all the text boxes on the form:

For Each ctl In Forms!Form1.Controls
If ctl.ControlType = acTextBox Then
ctl.OnMouseMove = "=MyFunc([" & ctl.Name & "])"
End If
Next

Function MyFunc(strControlName As String)

donaldg said:
Is there a way to get name of the control that triggered a MouseMove
event,
without explicitly passing the name?

The solutions recommended in Subject: Get name of a control, 5/16/2006
1:40
PM PST By: ScottH--- ,

to use Screen.ActiveControl or Me.ActiveControl won't work, the
MouseMove
control isn't necessarily the active control.
 
I haven't even looked, not at a desk w/Access, but is there
"controlAtPosition(x,y)" type of routine?
You could test the mouse coordinates and find the control underlying.
 
Allen--thanks again. You saved me a lot of time fiddling with controls, I
really appreciate you taking your time with this.

Allen Browne said:
You are right.

Change the middle line of PassName() to:
ctl.OnMouseMove = "=MyFunc(""" & ctl.Name & """)"

Or, change MyFunc() to accept the control and give its name:
Public Function MyFunc(ctl As Control)
Debug.Print ctl.Name
End Function

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

donaldg said:
Allen, when I tested your recommendation as below, strControlName returns
the
field contents rather than the control's name ie, instead of "txtTitle" it
returns "Once and Future King."

Sub PassName()
For Each ctl In Forms!MyBookList.Controls
If ctl.ControlType = acTextBox Then
ctl.OnMouseMove = "=MyFunc([" & ctl.Name & "])"
End If
Next
End Sub

Public Function MyFunc(strControlName As String)
Debug.Print strControlName
End Function

Allen Browne said:
Unfortunately, Access/VBA provides no way to know the name of the control
whose event is triggered, or the currently executing procedure.

If you have a bunch of controls you need to process, and you don't need
the
arguments (X, Y, etc.), you could programmatically assign the OnMouseMove
property of each control so it calls a generic function and passes the
control name. Assuming Form1 is open in design view, and assuming you
want
to do this for all the text boxes on the form:

For Each ctl In Forms!Form1.Controls
If ctl.ControlType = acTextBox Then
ctl.OnMouseMove = "=MyFunc([" & ctl.Name & "])"
End If
Next

Function MyFunc(strControlName As String)

Is there a way to get name of the control that triggered a MouseMove
event,
without explicitly passing the name?

The solutions recommended in Subject: Get name of a control, 5/16/2006
1:40
PM PST By: ScottH--- ,

to use Screen.ActiveControl or Me.ActiveControl won't work, the
MouseMove
control isn't necessarily the active control.
 
Back
Top