A control array in VBA like in VB6 is that possible?

Guus2005

AWF VIP
Local time
Today, 11:23
Joined
Jun 26, 2007
Messages
2,642
In VB6 is used control array's to dynamically create controls on a form. Is that possible in VBA?

I want to be able to create a paint program in VBA. I want to load line controls to write or draw anything on a form. I found this code on www.planetsourcecode.com made by Niloy Mondal
Code:
'An control array of line has been made. Its visiblity is set to false.
Dim ButtonPress As Boolean
Dim lastx, lasty As Integer

Private Sub Form_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
    ButtonPress = True
    lastx = x
    lasty = y
End Sub

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
   If ButtonPress = True Then
       Load Line1(Line1.Count)
       Line1(Line1.UBound).X1 = lastx
       Line1(Line1.UBound).X2 = x
       Line1(Line1.UBound).Y1 = lasty
       Line1(Line1.UBound).Y2 = y
       Line1(Line1.UBound).Visible = True
       lastx = x
       lasty = y
   End If
End Sub

Private Sub Form_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
   ButtonPress = False
End Sub
Thx!
 
Control arrays cannot be done in VBA like in VB6. You could create a multi-dimensional array storing control name and type then use the CreateControl Method to create new controls on a form...however this is complicated in Access as you can only create new controls on a form when it's opened in design view.
 
Didn't think it was possible. Thanks for your answer.
 
Also, it may be preferable to use Type instead of multi-dimensional array, then create a one-dimension array of that custom Type. Alternatively, use a class module to provide all the methods.
 
Also, it may be preferable to use Type instead of multi-dimensional array, then create a one-dimension array of that custom Type. Alternatively, use a class module to provide all the methods.
And using this method, it is possible to create controls at runtime?
 
No, DJKarl is right about having to open it in design view; it's a fact of the method itself, not how you store it.

But here's a way to make it look like it happened in runtime:

Use a subform. When you need a new control, set the subform container's sourceobject to nothing, then open that form in design view and hidden, add the control, save & close, then reset the container's sourceobject back to the subform with new control. Hopefully, all your users will see is a flicker on the screen, or a moment of blank-ness while this happens.
 
Last edited:

Users who are viewing this thread

Back
Top Bottom