You can't create a control array at runtime but you can 'simulate' one using a a class module and a collection class. Basically, the Class module is used to handle everything you need for a Command button. It receives the Button_Click events from the control and passes them on to its parent, the Collection class. The Collection class in turn raises the Event to the Form.

Pretty simple once you know the ins and outs. Probably overkil for this project but you can add as many events, properties, methods as you want.


VB Code:
  1. 'Class module to handle CommandButton Events
  2. Private WithEvents mobjCB As CommandButton
  3. Private mlngIndex As Long
  4. Private mobjParent As MyButtonsCollection
  5.  
  6. Public Property Set MyParentCollection(Value As MyButtonsCollection)
  7.     Set mobjParent = Value
  8. End Property
  9.  
  10. Public Property Get Index() As Long
  11.     Index = mlngIndex
  12. End Property
  13. Public Property Let Index(ByVal Value As Long)
  14.     mlngIndex = Value
  15. End Property
  16.  
  17. Public Property Get Button() As VB.CommandButton
  18.      Set Button = mobjCB
  19. End Property
  20.  
  21. Public Property Set Button(ButtonControl As VB.CommandButton)
  22.      Set mobjCB = ButtonControl
  23. End Property
  24.  
  25. Private Sub Class_Terminate()
  26.     Set mobjParent = Nothing
  27.     Set mobjCB = Nothing
  28. End Sub
  29.  
  30. Private Sub mobjCB_Click()
  31.     mobjParent.RaiseButtonEvent Me
  32. End Sub
  33.  
  34. 'Collection class module to handle multiple buttons and to raise events to the Form.
  35. 'Omitted the typical Collection properties/methods like Count, Item etc..
  36.  
  37. Private mCol As Collection
  38.  
  39. Public Event ButtonClicked(Sender As MyButton)
  40.  
  41. Public Function Add(Button As VB.CommandButton, Index As Long, Optional sKey As String) As MyButton
  42.     'create a new object
  43.     Dim objNewMember As MyButton
  44.     Set objNewMember = New MyButton
  45.  
  46.  
  47.     Set objNewMember.Button = Button
  48.     objNewMember.Index = Index
  49.    
  50.     If Len(sKey) = 0 Then
  51.         mCol.Add objNewMember
  52.     Else
  53.         mCol.Add objNewMember, sKey
  54.     End If
  55.     Set objNewMember.MyParentCollection = Me
  56.    
  57.     'return the object created
  58.     Set Add = objNewMember
  59.     Set objNewMember = Nothing
  60.  
  61. End Function
  62.  
  63. Private Sub Class_Initialize()
  64.     Set mCol = New Collection
  65. End Sub
  66.  
  67. Public Sub RaiseButtonEvent(ButtonObject As MyButton)
  68.     RaiseEvent ButtonClicked(ButtonObject)
  69. End Sub
  70.  
  71. 'Form Module
  72. 'Omitted the clean up code to Remove the Controls created at runtime, delete the collection etc...
  73. Private WithEvents mcolButtons As MyButtonsCollection
  74.  
  75. Private Sub Form_Load()
  76.  
  77.     Dim objButton As MyButton
  78.     Dim lngIdx As Long
  79.    
  80.     Set mcolButtons = New MyButtonsCollection
  81.    
  82.     For lngIdx = 0 To 63
  83.         Set objButton = mcolButtons.Add(Controls.Add("VB.CommandButton", "Command" & lngIdx), lngIdx, "Command" & lngIdx)
  84.        
  85.         With objButton.Button
  86.             .Caption = "Command" & lngIdx
  87.             .Visible = True
  88.             .Move 0, 315 * lngIdx, 2000, 315
  89.         End With
  90.     Next
  91.  
  92. End Sub
  93.  
  94. Private Sub mcolButtons_ButtonClicked(Sender As MyButton)
  95.     Debug.Print Sender.Button.Caption, Sender.Index
  96. End Sub