2

My question is basically answered in this post

But instead of the class code below:

Sub CmdEvents_Click()
   MsgBox "Hello World"
End Sub

I need to run this code (it's just a sample, I want to use the name of the button clicked later in the program)

Sub CmdEvents_Click()
Dim mvCtrl As Control
Set mvCtrl = Me.ActiveControl
MsgBox mvCtrl.Name
End Sub

But this code above will NOT, by any means return correctly the Name of the button I just created in run time in the message box.

It returns "Button 6".... I did a search in my workbook and I don't even have this name.

I actually I modified the original code from the link above to get the names of the buttons all from a list. This way I can modify the list, thus modifying the name of the buttons, or caption if I want to. And this Button 6 doesn't even exists in my list.

I made some traps in my code, showing message boxes for the variable I use to get the button name from, and it's correct, it's carrying the string I want to name my button before it creates it.

Community
  • 1
  • 1
  • How have you set the Button `Name`? The link shows an answer which only sets the `Caption` –  Sep 11 '12 at 19:56
  • 4
    Assuming you followed the post you linked to, your class instance already contains a variable `CmdEvents` which references the clicked button, so you can use `Msgbox CmdEvents.Name` If your code is different, it would be useful to post the relevant parts so we can see what's going on. – Tim Williams Sep 11 '12 at 20:15
  • Thanks Tim Williams you just totally nailed it. I really appreciate it. – Gilberto Melo Jr Sep 13 '12 at 13:53
  • @TimWilliams - please convert your comment into an answer so the user can accept it. – miroxlav Apr 10 '14 at 15:53

1 Answers1

1

Assuming you followed the post you linked to, your class instance already contains a variable CmdEvents which references the clicked button, so you can use

Msgbox CmdEvents.Name 

If your code is different, it would be useful to post the relevant parts so we can see what's going on.

Tim Williams
  • 154,628
  • 8
  • 97
  • 125