Thread: Class modules
View Single Post
  #4 (permalink)  
Old August 24th, 2005, 02:59 AM
gcianfanelli gcianfanelli is offline
Authorized User
Join Date: Jun 2003
Location: , , United Kingdom.
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts

to truly add a control at runtime in VBA you need to execute the Add method of the UserForm object's Controls property. This method expects the ProgID of the control you want to add - something like the following code should be added to your test UserForm:

Option Explicit

Private WithEvents mRuntimeButton As MSForms.CommandButton

Private Sub mRuntimeButton_Click()

  MsgBox "runtime button clicked"

End Sub

Private Sub UserForm_Initialize()

  Set mRuntimeButton = Me.Controls.Add("Forms.CommandButton.1", "mRuntimeButton", True)

End Sub
You'll additionally need to set a whole bunch of properties (Height, Width, Caption, etc) in order to get the button looking as you want it

If you need to know more, highlight the Add method in your VBA environment and hit F1 for some help