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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
gowflowCommented:
can you post the workbook maybe something else interveening ?
gowflow
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Office

From novice to tech pro — start learning today.