PC Review


Reply
Thread Tools Rate Thread

Adding controls to a userform at runtime

 
 
AlanGriffithKapitiNZ
Guest
Posts: n/a
 
      4th May 2009
I would like to generate various controls on a skeleton userform (eg, the
number may vary with the occasion) and have tried
Private Sub UserForm_activate()
Stop
Dim obj As Object
Set obj = Me
Set obj = obj.Add("MSForms.CommandButton.1", "Butt1", True)

but when the form opens I receive the error message
Runtime error '-2147221005(800401f3':
Invalid class string

What am I doing wrong, please?


 
Reply With Quote
 
 
 
 
OssieMac
Guest
Posts: n/a
 
      4th May 2009
Hi Alan,

Note: Space and underscore at the end of a line is a line break in an
otherwise single line of code.

Private Sub UserForm_Activate()

Dim Btn1 As MSForms.Control

On Error Resume Next
Set Btn1 = Me.Controls("Butt1")

If Btn1 Is Nothing Then
Set Btn1 = Me.Controls.Add _
("Forms.CommandButton.1", "Butt1", True)

With Btn1
.Caption = "My button"
.Top = 100
.Left = 100
.Height = 20
.Width = 100
End With
Else
'Insert your code here to handle if button already exists
MsgBox "Cannot Add Butt1. Already exists"
End If


End Sub


--
Regards,

OssieMac


"AlanGriffithKapitiNZ" wrote:

> I would like to generate various controls on a skeleton userform (eg, the
> number may vary with the occasion) and have tried
> Private Sub UserForm_activate()
> Stop
> Dim obj As Object
> Set obj = Me
> Set obj = obj.Add("MSForms.CommandButton.1", "Butt1", True)
>
> but when the form opens I receive the error message
> Runtime error '-2147221005(800401f3':
> Invalid class string
>
> What am I doing wrong, please?
>
>

 
Reply With Quote
 
OssieMac
Guest
Posts: n/a
 
      4th May 2009
Hi again Alan,

Amendment. Need to cancel the On Error. Insert in code as follows

On Error Resume Next
Set Btn1 = Me.Controls("Butt1")
On Error GoTo 0

--
Regards,

OssieMac


"OssieMac" wrote:

> Hi Alan,
>
> Note: Space and underscore at the end of a line is a line break in an
> otherwise single line of code.
>
> Private Sub UserForm_Activate()
>
> Dim Btn1 As MSForms.Control
>
> On Error Resume Next
> Set Btn1 = Me.Controls("Butt1")
>
> If Btn1 Is Nothing Then
> Set Btn1 = Me.Controls.Add _
> ("Forms.CommandButton.1", "Butt1", True)
>
> With Btn1
> .Caption = "My button"
> .Top = 100
> .Left = 100
> .Height = 20
> .Width = 100
> End With
> Else
> 'Insert your code here to handle if button already exists
> MsgBox "Cannot Add Butt1. Already exists"
> End If
>
>
> End Sub
>
>
> --
> Regards,
>
> OssieMac
>
>
> "AlanGriffithKapitiNZ" wrote:
>
> > I would like to generate various controls on a skeleton userform (eg, the
> > number may vary with the occasion) and have tried
> > Private Sub UserForm_activate()
> > Stop
> > Dim obj As Object
> > Set obj = Me
> > Set obj = obj.Add("MSForms.CommandButton.1", "Butt1", True)
> >
> > but when the form opens I receive the error message
> > Runtime error '-2147221005(800401f3':
> > Invalid class string
> >
> > What am I doing wrong, please?
> >
> >

 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      4th May 2009
I've always had better luck to add all the controls that I'll ever need while in
design mode.

But I'll hide the ones I don't need. Then I can use code to show the ones I
need when I need them.

It makes it easier for the code associated with those controls, too.

AlanGriffithKapitiNZ wrote:
>
> I would like to generate various controls on a skeleton userform (eg, the
> number may vary with the occasion) and have tried
> Private Sub UserForm_activate()
> Stop
> Dim obj As Object
> Set obj = Me
> Set obj = obj.Add("MSForms.CommandButton.1", "Butt1", True)
>
> but when the form opens I receive the error message
> Runtime error '-2147221005(800401f3':
> Invalid class string
>
> What am I doing wrong, please?


--

Dave Peterson
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
adding controls to userform at runtime Ouka Microsoft Excel Programming 3 18th Aug 2005 08:11 PM
Userform runtime controls & Macs =?Utf-8?B?SmltIENvd2Fu?= Microsoft Excel Programming 0 15th Jun 2005 07:21 PM
Adding controls to userform ExcelMonkey Microsoft Excel Programming 3 25th Feb 2005 11:30 AM
Adding Controls at runtime ? Thomas H. Microsoft C# .NET 8 5th Jan 2005 02:06 PM
Adding controls at runtime Michael S. Montoya Microsoft Access Forms 0 6th Aug 2003 05:37 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:36 AM.