0

I would like to be able to create forms as required, after my VBA program has started. Is this possible?

I have search stackoverflow and across the web and can't find a definitive answer to this. If it can be done, I haven't found the code to do it.

I'm just looking for some very basic, generic code to do this, if it is possible.

Harry Toes
  • 11
  • 3

1 Answers1

0

In general you can do what you're after, though you've left out a lot of specifics so it's hard to say.

Forms are just like any other class, so it's possible to instantiate many of them.

Public Sub CreateForms()

    Dim i As Integer

    For i = 1 To 3
        Dim u As UserForm1
        Set u = New UserForm1
        u.Show False ' <- Set modal to false so the new form will not block the loop.
    Next i

End Sub

This should create three copies of the same form.

EDIT

After reviewing Tim's comment, I've added the below.

Dim u As UserForm1
Set u = New UserForm1
Dim t As MSForms.CommandButton
Load u

With u
    Set t = u.Controls.Add("Forms.CommandButton.1", "cmdButton1", True)
    With t
        .Caption = "Dynamic Button"
        .Left = 10
        .Top = 10
    End With
    .Height = 300
    .width = 600
    .Caption = "Dyanmic Form"
    .Show
End With

It looks like you do need to have a blank form already created, but you could just have something simple to serve as the base of any form you wish to generate.

More info: http://msdn.microsoft.com/en-us/library/aa277578%28v=vs.60%29.aspx

Seth
  • 199
  • 9