Return the name of the object I click on in a form

J

John Dumay

Hi,

How do I go about returning the name of an object on a form. For example if
I set the "On Click" event procedure on a rectangle object to return:

MsgBox Me.Name

I get the form name. What I want to do is click on the object and be abe to
reference it's properties such as Tag to run other code based in these values.

As always any assistance is greatly appreciated.

Regards,

John Dumay
 
A

AG

For the name of the control try
Screen.ActiveControl.Name

For the control itself use
Screen.ActiveControl
 
J

John Dumay

Hi AG,

Thanks for the reply. Yes I have tried that option but the Rectangle object
cannot get the focus so it cannot return the name of the clicked object.
Instead it returns the name of the object which has focus. (In my case a
toggle button)

Regards,

John
 
A

AG

All I can think of would be to pass the name of the control to your
procedure.

For instance:
Private Sub Rectangle1_Click()
Call YourControlFunction("Box44") 'for controls that can't get focus,
pass the name
End Sub

Private Sub TextBox1_Click()
Call YourControlFunction 'for controls that can get focus, no need to
pass its name
End Sub

If you have a lot of controls, you can save some work by selecting all the
controls that can get focus and putting '=YourControlFunction()' in the 'On
Click' event.

Private Function YourControlFunction(Optional byval strControlName As String
= "")
Dim ctL as Access.Control
If strControlName <> "" Then
Set ctL = Me(strControlName)
Else
Set ctL = Screen.ActiveControl
End If
If ctL.ControlType = acTextBox Then
Do something
ElseIf ctL.ControlType = acComboBox Then
Do something else
End If
End Function
 
J

John Dumay

Hi AG,

Looks like a perfectly good solution. Will give it a go and let you know.

Thanks for your assistance.

Regards,

John Dumay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top