424 Error whem trying to change Visible property

E

ExcelMonkey

Why am I getting an Run time error 424 when trying to change the visible
properties in a control which is loaded into a collection. The collection is
dim'd as a Public variable. I am trying to change this property within a
click event of a button on the form

Public cntrlclctn1 As Collection

Private Sub CommandButton1_Click()
Dim x As Integer
For x = 1 To cntrlclctn1.Count
cntrlclctn1(x).Visible = True
Next

End Sub

Sub thing()
Dim lbl As Control
Dim txtbx As Control

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

Set txtbx = UserForm1.Controls.Add("Forms.TextBox.1")
txtbx.Text = "Text Box Text"
txtbx.Visible = False

'Add controls to collection
Set cntrlclctn1 = New Collection
cntrlclctn1.Add (lbl)
cntrlclctn1.Add (txtbx)

UserForm1.Show
End Sub
 
J

Jim Thomlinson

Remove the brackets...
Set cntrlclctn1 = New Collection
cntrlclctn1.Add lbl
cntrlclctn1.Add txtbx

With the brackets you are passing the default property instead of the object
itself...
 
E

ExcelMonkey

Thanks Jim.

EM

Jim Thomlinson said:
Remove the brackets...
Set cntrlclctn1 = New Collection
cntrlclctn1.Add lbl
cntrlclctn1.Add txtbx

With the brackets you are passing the default property instead of the object
itself...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top