Adding events to controls at runtime - VBA

I am trying to capture the click event of a button click that was created at runtime

I created a Class called Class1 with the following:

Public WithEvents buttonPress As MSForms.CommandButton
Private Sub buttonPress_Click()
    MsgBox "Hi"
End Sub

In the userform, I created a button control and add the click event  but when I click the button, there is no popup

Private Sub UserForm_Initialize()
Dim buttonCollection As New Collection
Dim buttonEvents As Class1
Dim button1 As Control

Set button1 = Me.Controls.Add("Forms.CommandButton.1")

Set buttonEvents = New Class1
Set buttonEvents.buttonPress = button1
buttonCollection.Add buttonEvents
End Sub
jnsimexAsked:
Who is Participating?
 
James ElliottManaging DirectorCommented:
This works for me.

Public WithEvents btn As MSForms.CommandButton

Private Sub btn_Click()

MsgBox "Hi"

End Sub

Private Sub UserForm_Initialize()

Set btn = UserForm1.Controls.Add("Forms.CommandButton.1", "example", True)

End Sub

Open in new window

0
 
gowflowCommented:
try changing this
Private Sub buttonPress_Click()
    MsgBox "Hi"
End Sub

to this
Public Sub buttonPress_Click()
    MsgBox "Hi"
End Sub

gowflow
0
 
jnsimexAuthor Commented:
i changed to it to:

Public Sub buttonPress_Click()
    MsgBox "Hi"
End Sub

But still no popup.

0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
gowflowCommented:
can you post the workbook maybe something else interveening ?
gowflow
0
 
James ElliottManaging DirectorCommented:
If you want to use a class, you need to encapsulate the entire button in that class, not just the events. You then pass the userform to the class via a property.



0
 
James ElliottManaging DirectorCommented:
Something like this for your Class:

Private WithEvents btn As MSForms.CommandButton
Private frmExample As UserForm

Property Set Form(oFrm As UserForm)

    Set frmExample = oFrm
    
End Property

Private Sub btn_Click()

    MsgBox "Hi"

End Sub

Public Sub Create()

    Set btn = frmExample.Controls.Add("Forms.CommandButton.1", "example", True)

End Sub

Open in new window


And this, behind your form:

Public oBtn As Class1 'your collection would go up here instead

Private Sub UserForm_Initialize()

Set oBtn = New Class1

Set oBtn.Form = Me

oBtn.Create

End Sub

Open in new window

0
 
jnsimexAuthor Commented:
@gowflow

here is the workbook

0
 
jnsimexAuthor Commented:
0
 
jnsimexAuthor Commented:
I was able to get my original code to work by declaring the collection at the modular level

@jell thank you for your reply. both examples worked perfectly but i have a quick question...i want to update a label based on the change event of the combobox.

how would I accomplish this? thanks.
0
 
James ElliottManaging DirectorCommented:
Your question isn't entirely clear.

Are either controls created dynamically?

Either way, the solution should be fairly clear based on the principles we've used with the Command Button.



0
 
jnsimexAuthor Commented:
let me ask in a new question.

thanks for your help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.