• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 807
  • Last Modified:

VBA: Get the name of the clicked control

Hi,

With a userform, I'd like to get the name of the clicked control automatically.
So for:

  Private Sub LeftHeaderBox2_Click()
    Set HeaderBox = Me.LeftHeaderBox2: Call FillProposalHeaderBox(HeaderBox)
 
   ' What can I replace 'Me.LeftHeaderBox2' with?
   ' Would think 'Me.ActiveControl' -- but 'Msgbox Me.ActiveControl.Name' names the frame that the
  '  label is in.

 End Sub

The current situation is only inconvenient because I have tons of clickable labels in my userforms.

Thanks for your help.
Michael



0
bishop3000
Asked:
bishop3000
  • 3
1 Solution
 
Antagony1960Commented:
Labels can't get the focus so it follows that they can never be the active control. The simplest way to do what you want is to use a module-wide string variable which you change in each label's click event, like so:

Dim sHeader as String

Private Sub lblHeader1_Click()
    sHeader = lblHeader.Caption
End Sub

0
 
Antagony1960Commented:
It's a pity VBA doesn't have control arrays a la VB proper as that would make it a piece of cake.
0
 
Antagony1960Commented:
The only other method I can think of would rely on the labels being uniformly distributed and sized on their parent controls, in which case you could use the X and Y coordinates of the parent control's MouseDown event to calculate which label has been clicked on and set the string accordingly from a predefined array. But that might end up being as much work as setting the string in the labels' click events. And such a method would add a big overhead to the task if you ever needed to reposition the labels.
0
 
bishop3000Author Commented:
Hi Antagony1960,
Thanks for your feedback.
Definitely a moment of impotence for me...when the name of the control is in the procedure title itself but VBA won't tell me the control's name.
Regards,
Michael
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now