Repost: Add Control to UserForm

LaShark

New Member
Joined
Mar 17, 2002
Messages
9
Once you add a control to a userform during runtime, how to activate/access the events such as _click()?

These controls work at runtime, such as a checkbox toggling on and off but I can not program an event.

Thanks for any help
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
You can add code at run time as well. You need to set a reference to Visual Basic Extensibility, but, I saw, I think Damon Ostrander, make a suggestion, like this:

If you know you MAY add a checkbox at run time, and you need the_Click() sub, you can create the Sub at Design time with the name of the control you will have at run time, that way, if the control is added, it'll have its _Click() event.
 
Upvote 0
Juan: Thanks for looking into this.

** I am not having any trouble adding a control, such as a checkbox, at run time. I am able to add the control with the following statements. **

Dim TestCheckbox As Control

Sub Private Whatever()

Set TestCheckbox = _ Controls.Add("Forms.Checkbox.1", _ "TestCheckbox", True)

End Sub

** Now if I add the following sub routine, it is treated as a standalone subroutine, not an event associated with the checkbox: **

Sub Private TestCheckbox_Click()
End Sub

** To further complicate matters, I tried the following test. **

Dim WithEvents SingleButton As CommandButton

Sub Private Whatever()

Set SingleButton = _ Controls.Add("Forms.CommandButton.1", _ "SingleButton", True)

End Sub

Sub Private SingleButton_Click()
SingleButton.Cpation = "Changed"
End Sub

** This works. The VBA compiler will allow
"Dim WithEvents SingleButton As CommandButton" but not Dim WithEvents SingleButton As CheckBox" **

Any clues?
This message was edited by LaShark on 2002-03-27 08:10
This message was edited by LaShark on 2002-03-27 08:21
 
Upvote 0

Forum statistics

Threads
1,214,644
Messages
6,120,709
Members
448,983
Latest member
Joaquim_Baptista

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top