retrieve name of object user clicks on

ruggles

New member
Local time
Today, 09:23
Joined
Apr 8, 2012
Messages
4
I know how to use "Screen.ActiveControl" to retrieve the name of a control that the user clicks on, but how do you retrieve the name of some other object like a rectangle? Screen.ActiveControl does not seem to work for that. I have a form with about 50 rectangles, and I want to retrieve the name of the specific rectangle that the user clicks on. Is this possible?
 
Off-hand, I'd suggest to, in its click event, store its name somewhere in a variable or hidden txtbox, or pass it to whatever code you are calling from there.
 
Thanks for the suggestion, but I was wondering if there was something equivalent to "Screen.ActiveControl"? I was hoping to make a single function that I could assign to the "On Click" event of all the rectangles so that the single function would return the name of whatever the user clicked on. Your way will work, but I would have to write a line of code for each rectangle (somewhere between 50 and 100 on the form).
 
I don't know how else to do it, maybe somebody else does?

As to writing code 50-100 times, I'd copy the module text into Word, and there make a macro to add appropriate line(s) of code to each event handler.
 
Don't know if this will save you time or not but what you really should do in this situation is put an invisible button behind your rectangle. You can use ActiveControl.Name on buttons.
 
Both great ideas, thanks! I'll play around with both ideas and see which one I like best. But life would be simpler if only a rectangle were a "control"!

Can someone explain why a command button object is considered a "Control" but not a rectangle object?
 
Last edited:
In design mode, select all the rectangles and type IncludeForClick into their Tag property.

The following code will then show the name of the rectangle which is clicked.
Code:
Private Sub Form_Open(Cancel As Integer)
    Dim ctl As Control
    
    For Each ctl In Me
        If ctl.Tag = "IncludeForClick" Then
            ctl.OnClick = "=HandleClick('" & ctl.Name & "')"
        End If
    Next ctl
    
End Sub


Public Function HandleClick(ByVal strCtlName As String)

    MsgBox strCtlName
    
End Function

Chris.
 
In design mode, select all the rectangles and type IncludeForClick into their Tag property.

The following code will then show the name of the rectangle which is clicked.
Code:
Private Sub Form_Open(Cancel As Integer)
    Dim ctl As Control
    
    For Each ctl In Me
        If ctl.Tag = "IncludeForClick" Then
            ctl.OnClick = "=HandleClick('" & ctl.Name & "')"
        End If
    Next ctl
    
End Sub


Public Function HandleClick(ByVal strCtlName As String)

    MsgBox strCtlName
    
End Function

Chris.


Great idea. Adding this to my personal bag-o-tricks.
 
Is the rectangle transparent? if so, you can only click the border. set it to some colour (eg white), and if necessary, do format/send to back.

then you will find the rectangle is now clickable
 
Technically speaking, all the rectangles can have a transparent BackStyle.

Only the rectangle which is clicked needs a solid BackStyle.
The rectangles MouseMove event can set the rectangles BackStyle to solid in anticipation of the impending Click.

Code:
Private Sub Form_Open(Cancel As Integer)
    Dim ctl As Control
    
    For Each ctl In Me
        If ctl.Tag = "IncludeForClick" Then
            ctl.OnClick = "=HandleClick('" & ctl.Name & "')"
            ctl.OnMouseMove = "=HandleMouseMove('" & ctl.Name & "')"
        End If
    Next ctl
    
    Me.Section(acDetail).OnMouseMove = "=HandleMouseMove('Detail')"
    
End Sub


Public Function HandleClick(ByVal strCtlName As String)

    MsgBox strCtlName
    
End Function


Public Function HandleMouseMove(ByVal strCtlName As String)
    Dim ctl As Control
    
    For Each ctl In Me
        If ctl.Tag = "IncludeForClick" Then
            ctl.BackStyle = 0
        End If
    Next ctl
    
    If strCtlName <> "Detail" Then
        Me(strCtlName).BackStyle = 1
    End If
    
End Function

Chris.
 
If the rectangles were transparent, there would be no reason to use a rectangle at all. You would just use a button instead.
 
Well, we don’t know what they are being used for so we can’t say for sure.

However, a transparent command button can’t have a solid border but a rectangle can.

So, for instance, if we wanted to define several areas of a map background by placing transparent buttons on it we would not be able to see the outer limits of the button. If we use a rectangle with transparent back style, but with a solid border, we can see the limits and the map beneath it.

But who knows what they are being used for if, indeed, they need to be used at all?

Chris.
 
the point i was making was that the OP might have problems clicking a rectangle IF it was set to transparent. His code may well be working correctly, otherwise for what he wants to do.

I mean you can click a (blank) section of a form if you want to, can you not. I can't imagine why you might want to do so, but I expect somebody might find a use for it. Alternatively, maybe it's just a "feature" of the object/collection model used to store all the objects that make up a form. ie, that just about everything is clickable
 
Wow! Chris, this is great. Thanks. If you ever come to Seattle I'll buy you dinner.
 
If you pay for the trip to Seattle I’ll buy you dinner. :D

Chris.
 

Users who are viewing this thread

Back
Top Bottom