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
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