Control Arrays - An easy one for you experts

Hi All,

I am cycling through the controls on a form using the below code ;


Dim cX As Control

For Each cX In Me
If Left(cX.Name, 7) = "ShapeD" & dNo Then
For sNo = 2 To lblMaxProductSessions
Load cX(sNo)
cX(sNo).Visible = True
Next sNo
End If
Next

The problem is I'm getting a runtime error 344, Must specify index for object array.

The object in question is a control array and the variable sNo is the new index number, any ideas why this won't work ?, do I have to maybe use the Scripting Control and if so how ?
[770 byte] By [Judgey] at [2007-11-18 21:56:31]
# 1 Re: Control Arrays - An easy one for you experts
I notice you are starting the index at 2. The index numbers must follow, so this will only work if you already have 2 of these controls on the form with index's set to 0 and 1 respectively.
Bill Crawley at 2007-11-9 23:02:00 >
# 2 Re: Control Arrays - An easy one for you experts
Hi,
Replace For Each cX In Me into For Each cX In Me.controls and try.

Madhivanan
Madhi at 2007-11-9 23:03:06 >
# 3 Re: Control Arrays - An easy one for you experts
Cheers Guy's

The Index on the first Control is 1 so hence starting the loop from 2. I have tried changing the loop to Me.Controls and makes no difference, still same error.

I think the problem lies with the

Load cX(sNo)

I think It thinks "cX" is a control name, not the actual value for cX which is ShapeD1T1.

ShapeD1(1) already exists, I am trying to achieve
ShapeD1(2) using the above as I need to do the same on other controls such as ShapeD2(1), ShapeD3(1) etc, etc.

Any more ideas guy's, I'm getting close to deadlines now..., which is 5pm today 8(
Judgey at 2007-11-9 23:03:59 >
# 4 Re: Control Arrays - An easy one for you experts
Load cX.Name(sNo)
Twodogs at 2007-11-9 23:04:59 >
# 5 Re: Control Arrays - An easy one for you experts
Cheers TwoDogs,

Still the same error though...
Judgey at 2007-11-9 23:06:00 >
# 6 Re: Control Arrays - An easy one for you experts
Load cX.Name(cx.Ubound +1)
'Now Ubound is this loaded control
cX.Name(cx.Ubound).visible = true
Cimperiali at 2007-11-9 23:07:10 >
# 7 Re: Control Arrays - An easy one for you experts
Originally posted by Cimperiali
Load cX.Name(cx.Ubound +1)
'Now Ubound is this loaded control
cX.Name(cx.Ubound).visible = true

Hi Cimperiali,

Tried the Load cX.Name(cx.Ubound +1) and get the error message :

"Object doesn't support this property or method"

Also tried ;

Load CX.Name(2) and
Load CX.Name(Sno) and get the original error ;

"Must specify index when using property array"

Surely there must be a way of loading a control this way ?
Judgey at 2007-11-9 23:08:09 >
# 8 Re: Control Arrays - An easy one for you experts
Try putting the name into a variable, then use the variable

ie
Dim tmpName as string
tmpName = cX.Name(sNo)
Load tmpName
Twodogs at 2007-11-9 23:09:13 >
# 9 Re: Control Arrays - An easy one for you experts
Cheers Two Dogs, but that don't work either,

Cheers for ally our help guys, but I'm going to have to leave it, project already 2 days overdue, I will have to fix the controls and if they change there mind in the future, right extra code and re-deploy new EXEs.

thanks again for all you help, shame we didn't crack it, but if anybody does find a solution, I'd really appreciate it finding out how to do it, I hate things beating me....

;)
Judgey at 2007-11-9 23:10:10 >
# 10 Re: Control Arrays - An easy one for you experts
Matter is:
when you make the for each, cx iis an element of the array
thus it has an index property but has not a ubound one...
Referring to the element of that array via Cx does not need the
Index (ie: you can write Cx.Visible= true, but can not write
Cx(Index).visible= true)
Thus you cannot load a Cx(sNo) but only
a Shape1(sNo)...

Private Sub CmdLoad_Click()
Dim cX As Control
Dim Counter As Integer
Dim shapeX As Shape
Dim theIndex As Integer

For Each cX In Me.Controls
If Left(cX.Name, 5) = "Shape" Then
For Counter = 1 To 10

Load Shape1(Shape1.UBound + 1)
Set cX = Shape1(Shape1.UBound)
cX.Move Shape1(Shape1.UBound - 1).Left + Shape1(Shape1.UBound - 1).Width
cX.Visible = True
Next Counter
End If
Next
End Sub
Cimperiali at 2007-11-9 23:11:12 >
# 11 Re: Control Arrays - An easy one for you experts
Cheers Cimperiali

That code does indeed work, but unfortunately it still will not work with my example, So does this mean I just cannot do what I need it to ?

Your example like mine checks the first 5 letters of the control name

If Left(cX.Name, 5) = "Shape" Then...

this is because I have a Control Array Shape named for each Day ;

Eg.
Shape1(1) 'Monday
Shape2(1) 'Tuesday
Shape3(1) 'Wednesday
Shape4(1) 'Thursday
Shape5(1) 'Friday

(I also have 7 time slots during the day, hence the Shape1T1(1), Shape1T2(1) etc, etc )

Only problem is the code still needs the Actual Control Name to in the ;

Set cX = Shape1(Shape1.UBound)

This means My code is going to be huge as each one will have to be named individually in the loop. Does this mean I will have to repeat your code 35 times changing the Set Statement to include the original Shape name ?

cheers M8, I'm really sorry to be a pain...
Judgey at 2007-11-9 23:12:06 >
# 12 Re: Control Arrays - An easy one for you experts
First, I do not understand what these shapes array are for (=are you wroking with Vb or with Vba?)
Second, I do not fully understand why you said the code I provided does not work (look at attachment).
Cimperiali at 2007-11-9 23:13:10 >
# 13 Re: Control Arrays - An easy one for you experts
Hi Cimperiali

I did say your code worked, just not with my example. I'll try and explain the problem using your Project example ;

1) If you take the Control Shape1(1) in your project and copy and paste it, it becomes Shape1(0)

2) Rename Shape1(0) to Shape2(1) and place it directly below Shape1(1),

3) now run and click the Load shapes button, Shape1(1) is indeed repeated but shape2 is not touched.

this is because you are naming shape1 in the ;

Set cX = Shape1(Shape1.UBound)

I have attached a screen shot of the app, as you can see I have 5 days lists and 7 Time slots. Within each time slot there can be a different number of session, for example 10:00-11:00 there are 4 and 9:00 - 10:00 there are 2.

I would like these to be customised by the user so this is why I'm trying to use the loop. I have not named each day Shape1 and increased the index, but have a different shape name for each day,

Shape1(1) 'Monday
Shape2(1) 'Tuesday
Shape3(1) 'Wednesday
Shape4(1) 'Thursday
Shape5(1) 'Friday

I want to perform exactly what you have done in the project example you sent me, but for every day not just day 1 ( or Shape1 ), but I want to be able to do this without having to create the For Next Statement in your example for every day and Hour slot.

I'm really sorry, its hard to explain, I know its impossible to help someone if you don't understand what they're trying to do, and I'm not the best at explaining myself :(
Judgey at 2007-11-9 23:14:11 >
# 14 Re: Control Arrays - An easy one for you experts
Have not time to build an example, but I thing you should play with collections.
That is: you could have a shapeObject that is contained in a
Day_collection (as many shapeObjects as you need per day),
and have a week_collection of the previous Day_collection
Then you could cycle for each item in the week_col
to get day_cols, and inside cylce for each Shaape in a day_col

Does it sound good?
Cimperiali at 2007-11-9 23:15:13 >
# 15 Re: Control Arrays - An easy one for you experts
The picture you attached is a bit too small : I cannot understand
what you're trying to achieve. You could take and zip a larger
one...
What are the shapes? Are they green squares?
Why not use pictures instead of shapes?
Cimperiali at 2007-11-9 23:16:19 >