Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 553
Default Run time error 91 when trying to add controls to a collection

Wy am I getting Run time error 91 when trying to add controls to my collection.

Public cntrlclctn1 As Collection
Sub thing()
Dim lbl As Control
Dim txtbx As Control

'Create controls
Set lbl = UserForm1.Controls.Add("Forms.Label.1")
lbl.Caption = "Label Text"
Set txtbx = UserForm1.Controls.Add("Forms.TextBox.1")
txtbx.Text = "Text Box Text"

'Add controls to collection
cntrlclctn1.Add (lbl)
cntrlclctn1.Add (txtbx)

UserForm1.Show
End Sub
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,939
Default Run time error 91 when trying to add controls to a collection

You have declared a variable for the collection but you have not actually
created the collection object. So you are trying to add controls to a
collection object that as yet does not exist... Add this line...

set cntrlclctn1 = new collection
--
HTH...

Jim Thomlinson


"ExcelMonkey" wrote:

Wy am I getting Run time error 91 when trying to add controls to my collection.

Public cntrlclctn1 As Collection
Sub thing()
Dim lbl As Control
Dim txtbx As Control

'Create controls
Set lbl = UserForm1.Controls.Add("Forms.Label.1")
lbl.Caption = "Label Text"
Set txtbx = UserForm1.Controls.Add("Forms.TextBox.1")
txtbx.Text = "Text Box Text"

'Add controls to collection
cntrlclctn1.Add (lbl)
cntrlclctn1.Add (txtbx)

UserForm1.Show
End Sub

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 553
Default Run time error 91 when trying to add controls to a collection

Was missing the following:

Set cntrlclctn1 = New Collection

Fixed!

Thanks

EM

"ExcelMonkey" wrote:

Wy am I getting Run time error 91 when trying to add controls to my collection.

Public cntrlclctn1 As Collection
Sub thing()
Dim lbl As Control
Dim txtbx As Control

'Create controls
Set lbl = UserForm1.Controls.Add("Forms.Label.1")
lbl.Caption = "Label Text"
Set txtbx = UserForm1.Controls.Add("Forms.TextBox.1")
txtbx.Text = "Text Box Text"

'Add controls to collection
cntrlclctn1.Add (lbl)
cntrlclctn1.Add (txtbx)

UserForm1.Show
End Sub

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,452
Default Run time error 91 when trying to add controls to a collection

Try this:

Sub thing()

Dim lbl As Control
Dim txtbx As Control

'Create controls
Set lbl = UserForm1.Controls.Add("Forms.Label.1")
lbl.Caption = "Label Text"

Set txtbx = UserForm1.Controls.Add("Forms.TextBox.1")
txtbx.Text = "Text Box Text"
txtbx.Left = lbl.Width + 10

If cntrlclctn1 Is Nothing Then
Set cntrlclctn1 = New Collection
End If

'Add controls to collection
cntrlclctn1.Add lbl
cntrlclctn1.Add txtbx

UserForm1.Show

End Sub


RBS


"ExcelMonkey" wrote in message
...
Wy am I getting Run time error 91 when trying to add controls to my
collection.

Public cntrlclctn1 As Collection
Sub thing()
Dim lbl As Control
Dim txtbx As Control

'Create controls
Set lbl = UserForm1.Controls.Add("Forms.Label.1")
lbl.Caption = "Label Text"
Set txtbx = UserForm1.Controls.Add("Forms.TextBox.1")
txtbx.Text = "Text Box Text"

'Add controls to collection
cntrlclctn1.Add (lbl)
cntrlclctn1.Add (txtbx)

UserForm1.Show
End Sub


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
UserForm Controls Collection Spencer Excel Programming 2 March 17th 07 05:49 PM
Run time error 13 with new Collection Tomkat743 Excel Programming 2 April 19th 06 12:46 PM
Collection from Range, tag cells on error nowon Excel Programming 1 January 3rd 06 12:34 AM
Create a collection of controls quartz[_2_] Excel Programming 7 November 2nd 05 10:16 PM


All times are GMT +1. The time now is 10:59 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"