PDA

View Full Version : Solved: Making a Collection of some Form Controls



SamT
04-28-2008, 08:51 PM
I've been trying to use a Collection of Frames (controls) to iterate just thru the Frames on the form instead of all the controls.

The "Collection_Of_Frames" Function does return a collection with the correct count. If I "Collect" Names I can access them as Strings, but when I try to get the Frames, I don't know what I am getting because I don't know what I'm doing:banghead: .

If I get this to work I'll add Vars so it can be called for any Form.
i.e. Collection_Of_frames(MyForm_Var As UserForm, MyNamePart_Var As String) as Collection. This is just so I can figger things out without running my entire form and all its initializing code.


Function Collection_Of_Frames() As Collection
'assumes UserForm1 Contains 1 or more Frames

Dim MyCtrl As Control
Dim MyFrames As New Collection

For Each MyCtrl In UserForm1.Controls
If MyCtrl.Name Like "Frame*" Then
MyFrames.Add (MyCtrl)
End If
Next MyCtrl


Set Collection_Of_Frames = MyFrames

End Function



But I can't seem to access any of the "whatevers" in the collection. In the following, I have commented out the lines that don't work with the error msgs at the ends of the lines.


Sub Test()
Dim MyName As String
Dim MyCollection As New Collection
Dim MyFrame As Frame
Dim MyCheck As Boolean
Dim MyString As String
Dim MyObject As Object
Dim MyCtrl As Control
Dim MyCount As Long

Set MyCollection = Collection_Of_Frames
MyCount = MyCollection.Count 'Count is right

Set MyObject = MyCollection.Item(1)
MyCheck = MyObject Is MyCollection(1) 'MyCheck = true

'Set MyFrame = MyCollection.Item(1) ' Error = Type Mismatch
'Set MyCtrl = MyCollection(1) 'Error = Type Mismatch
'MyString = MyCollection.Item(1) ' Error = Invalid Property Assignment
'MyName = MyCollection.Item(1).Name 'Error = Object doesn't support this Property or Method.
'Set MyCollection(1).BackColor = 14794198 'Error = Object doesn't support this Property or Method.
'Set MyCollection(1).Caption = "This is a Frame" 'Error = Object doesn't support this Property or Method.

End Sub


I did use something like

For each MyCtrl in UserForm.controls
If MyCtrl.Name Like *Frame* Then
Set MyFrame = MyCtrl
End If
For Each MyCtrl in MyFrame.Controls
Do Something
next
next

but I have to do this in several procedures and it just seems more elegant to use a function.

Please help :(
SamT

Bob Phillips
04-29-2008, 12:44 AM
Is thgis any good for you?

First create a clas called clsUserFormEvents with this code



Public WithEvents mFrameGroup As msforms.Frame


Set up the class like this



Dim cFrameEvents As clsUserFormEvents
Dim ctl As msforms.Control

Set mcolEvents = New Collection

For Each ctl In Me.Controls
If TypeName(ctl) = "Frame" Then
Set cFrameEvents = New clsUserFormEvents
Set cFrameEvents.mFrameGroup = ctl
mcolEvents.Add cFrameEvents
End If
Next
Set cFrameEvents = Nothing


and access like so



MsgBox mcolEvents.Item(1).mFrameGroup.Name
MsgBox mcolEvents.Item(2).mFrameGroup.Name

rory
04-29-2008, 06:05 AM
SamT,
Your function will work if you use:
Function Collection_Of_Frames() As Collection
'assumes UserForm1 Contains 1 or more Frames

Dim MyCtrl As msforms.Control
Dim MyFrames As New Collection

For Each MyCtrl In Me.Controls
If TypeName(MyCtrl) = "Frame" Then
MyFrames.Add MyCtrl, MyCtrl.Name
End If
Next MyCtrl


Set Collection_Of_Frames = MyFrames

End Function

Your problem was the parentheses around MyCtrl when you added the frame to the collection - they dereference the control so you end up adding its name instead.

SamT
04-29-2008, 08:02 AM
Thanks guys,

I'll give these a try and let you know how it comes out.

Probably be a couple of hours before I can work on it, though.

SamT

SamT
04-30-2008, 06:07 PM
XLD, I went with Rory's solution, because, well...frankly I'm too green to understand what you did.:doh:

Rory, Thanks and thanks for the insight on TypeName and the use of Object as its parameter. Yes, I will remember to try ()'s when nothing seems to work with Objects.

What you showed me inspired me to expand the function's abilities and now it will collect by control type and/or name segment.

I have attached the results, but as you can see, I can't figure out how to test for the presence of an actual UserForm in the first function parameter.:dunno

If someone thinks it's worth it and wants to error proof it, I give permission to distribute it for free thru VBAExpress.com. :bow:

Again, thanks to both a ya! :friends: :friends:

SamT

Ps: I can't recall if I should have edited the "How To" comments. Oh well...