VB CreateObject or Controls.Add

msvec

Board Regular
Joined
Feb 18, 2002
Messages
202
I'm trying to create a control dynamically, and while i've tried several things, nothing works...any ideas...here's what i've tried, and here are the errors i've received:

Set lblVar2 = Controls.Add("VB.Label", "lblVar2", True)

Error: Invalid Class String

Set lblVar2 = CreateObject("userForm.Label1")

Error: ActiveX component can't create object

any help would be most appreciated. thanks
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Hi msvec, Here is a sample I made for you.
If you want to add only controls...please try this.

<pre>
<Font color=blue>'_/_/_/Sample1_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/</font>
'UserForm Module

Dim MyLabel1 As MSForms.Label

Private Sub UserForm_Initialize()
Set MyLabel1 = Controls.Add("Forms.Label.1")
MyLabel1.Caption = "What's up"
End Sub

<Font color=blue>'_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/</font>
</pre>

Or you want to use "Event" of controls, we can use "WithEvents" when
we declare variables like this. ..please try this too.

<pre>
<Font color=blue>'_/_/_/Sample2_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/</font>
'UserForm Module

Private WithEvents MyLabel1 As MSForms.Label

Private Sub MyLabel1_Click()
MsgBox "Cliked"
End Sub

Private Sub UserForm_Initialize()
Set MyLabel1 = Controls.Add("Forms.Label.1")
MyLabel1.Caption = "What's up"
End Sub
<Font color=blue>'_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/</font>
</pre>
 
Upvote 0
Thanks you two...most helpful there colo...i never would have come up with that config. what does the "1" represent in ("Forms.Label.1") ??

just curious, but this works great for me...thanks.
 
Upvote 0
Hi msvec,
The "1" represent in ("Forms.Label.1") is ProgID as follows(please see your help).

Syntax
Set Control = object.Add( ProgID [, Name [, Visible]])

Settings

ProgID values for individual controls are:

CheckBox Forms.CheckBox.1
ComboBox Forms.ComboBox.1
CommandButton Forms.CommandButton.1
Frame Forms.Frame.1
Image Forms.Image.1
Label Forms.Label.1
ListBox Forms.ListBox.1
MultiPage Forms.MultiPage.1
OptionButton Forms.OptionButton.1
ScrollBar Forms.ScrollBar.1
SpinButton Forms.SpinButton.1
TabStrip Forms.TabStrip.1
TextBox Forms.TextBox.1
ToggleButton Forms.ToggleButton.1
 
Upvote 0

Forum statistics

Threads
1,215,059
Messages
6,122,917
Members
449,093
Latest member
dbomb1414

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