View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
Bob Kilmer Bob Kilmer is offline
external usenet poster
 
Posts: 280
Default Iterate over Collection Objects in Container.

Is it possible to Set an object to a string reference?

No. A String is not an object in VB. You can *add* a String to a Collection
or assign a String to a String or Variant property of an object..

collectionobject.Add "some string"

label.Caption = "some string"

--
Bob Kilmer


"Paul M" wrote in message
...
Hi,

I have a question that I hope can be answered quickly.
I'm sure there must be a way of doing it.

I have three Public Collections dimmed in a Sub.
I have a For Each statement iterating over the controls
on a form returning List Boxes with Selected Items. I can
get the controls to iterate, but can't get the
Collections to change as well.

Basically, the Control Name is the Collection Name Mid(4)

So:

Control Name = lbxOwner
Collection Name = Owner or Preferably colOwner if
possible to add col & the mid statement.

The problems is I dim the Control.Name as a variable
called ActColName as String, but I can't set the object
(ActCollection) as Collection because of the string
dimension.

Is it possible to Set an object to a string reference?

In the immediate window if you type:

Set actCollection = colOwner
It works
But you get an Object Error if you try
Set actCollection = ActColName (Where ActColName =
colOwner as String)


Here is the sample code (Including only the Dimensions
referenced here)
Public colOwner As New Collection
Public colCoOrdinator As New Collection
Public colAdministration As New Collection
Dim Item As Collection
Dim ListIndex As Variant
Dim i As Control
Dim ActControl As Object
Dim ActCollection As Collection
Dim ActColName As Variant
Dim itemSelected As Boolean
For Each i In fmDivReport.Controls
If i.Name Like "lbx*" Then
ActColName = "col" & Mid(i.name, 4)
Set ActControl = i
Set ActCollection = ActColName ' This is
where it errors
For ListIndex = 0 To ActControl.listCount - 1
If ActControl.Selected(ListIndex) = True
Then
ActCollection.Add ListIndex
Debug.Print ActControl.Name & " " &
ActControl.List(ListIndex)
itemSelected = True
End If
Next ListIndex
End If
ListIndex = 0
Next i

-----
Any help would be great.

Many Thanks in Advance

Paul M.