Converting a collection into a control array
Here is a simple example of converting from a collection of controls into a control array.
Personally I much prefer working with control arrays than using control enumeration, so thats why I wrote this.
This example does however use control enumeration to find all the controls in the first place.
Create a form, and place 6 command buttons on it.
If prompted, do not use a control array.
VB Code:
Private Function createControlArray(ByRef controlCollection As Object) As Object()
If Not controlCollection.Count = 0 Then
Dim retVal() As Object: ReDim retVal(controlCollection.Count)
Dim i As Long: For i = 0 To UBound(retVal) - 1: Set retVal(i) = controlCollection.Item(i): Next
createControlArray = retVal
End If
End Function
Private Sub Form_Load()
Command1.Caption = "Command1": Command2.Caption = "Command2": Command3.Caption = "Command3":
Command4.Caption = "Command4": Command5.Caption = "Command5": Command6.Caption = "Command6":
Dim cmdButtons() As Object: cmdButtons = createControlArray(Form1.Controls)
MsgBox "Caption of third command button : " & cmdButtons(3).Caption
End Sub