Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create User Form Using VBA

VBA How To

Create User Form Using VBA

by  PBAPaul  Posted    (Edited  )
Sometimes you need to create a complex UserForm with many components. I had to create a form with 3 labels, 1 text box and a check box for 43 different records (215 separate components) and so developed this technique of programatically creating the form.

Obviously one can manually create a complex UserForm but the problem of individually naming each component and writing the event handler codes is a real drag.

The references that need to be loaded are:
Visual Basic For Applications
Microsoft Excel Object Library
MS Forms 2.0 Object Library

The code below is based on John Walkenbach's original procedure.

Code:
Sub MakeUserForm()
    Dim TempForm As Object
    Dim NewButton As MSForms.commandbutton
    Dim NewLabel As MSForms.Label
    Dim NewTextBox As MSForms.TextBox
    Dim NewOptionButton As MSForms.OptionButton
    Dim NewCheckBox As MSForms.CheckBox
    Dim X As Integer
    Dim Line As Integer
    Dim MyScript(4) As String

    'This is to stop screen flashing while creating form
    Application.VBE.MainWindow.Visible = False

    Set TempForm = ThisWorkbook.VBProject.VBComponents.Add(3)

    'Create the User Form
    With TempForm
        .Properties("Caption") = "My User Form"
        .Properties("Width") = 450
        .Properties("Height") = 300
    End With

    'Create 10 Labels
    For X = 0 To 9
        Set NewLabel = TempForm.designer.Controls.Add("Forms.label.1")
        With NewLabel
            .Name = "FieldLabel" & X + 1
            .Caption = "My Label " & X + 1
            .Top = 20 + (12 * X)
            .Left = 6
            .Width = 90
            .Height = 12
            .Font.Size = 7
            .Font.Name = "Tahoma"
            .BackColor = &H80FFFF
        End With
    Next

    'Create 10 Text Boxes
    For X = 0 To 9
        Set NewTextBox = TempForm.designer.Controls.Add("Forms.textbox.1")
        With NewTextBox
            .Name = "MyTextBox" & X + 1
            .Top = 20 + (12 * X)
            .Left = 100
            .Width = 150
            .Height = 12
            .Font.Size = 7
            .Font.Name = "Tahoma"
            .BorderStyle = fmBorderStyleSingle
            .SpecialEffect = fmSpecialEffectFlat
        End With
    Next

    'Create 10 Check Boxes
    For X = 0 To 9
        Set NewCheckBox = TempForm.designer.Controls.Add("Forms.checkbox.1")
        With NewCheckBox
            .Name = "MyCheck" & X + 1
            .Caption = ""
            .Top = 20 + (12 * X)
            .Left = 260
            .Width = 12
            .Height = 12
            .Font.Size = 7
            .Font.Name = "Tahoma"
            .BackColor = &HFF00&
        End With
    Next

    'Create 10 Labels -> result of Check Box
    For X = 0 To 9
        Set NewLabel = TempForm.designer.Controls.Add("Forms.label.1")
        With NewLabel
            .Name = "Result_Text" & X + 1
            .Caption = ""
            .Top = 20 + (12 * X)
            .Left = 280
            .Width = 150
            .Height = 12
            .Font.Size = 7
            .Font.Name = "Tahoma"
            .BackColor = &H80FFFF
        End With
    Next

    'Create Event Handler Code For Each Check Box
    '(True -> Upper Case of Text Box Value;False -> Lower Case of Text Box Value)
    For X = 0 To 9
        With TempForm.codemodule
            Line = .countoflines
            MyScript(0) = "Sub MyCheck" & X + 1 & "_Click()"
            MyScript(1) = "If .MyCheck" & X + 1 & " = true then"
            MyScript(2) = ".result_Text" & X + 1 & ".caption = ucase(.mytextbox" & X + 1 & ".value)"
            MyScript(3) = ".result_Text" & X + 1 & ".caption = lcase(.mytextbox" & X + 1 & ".value)"
            .insertlines Line + 3, MyScript(0)
            .insertlines Line + 2, "With Me"
            .insertlines Line + 3, MyScript(1)
            .insertlines Line + 4, MyScript(2)
            .insertlines Line + 5, "Else"
            .insertlines Line + 6, MyScript(3)
            .insertlines Line + 7, "End if"
            .insertlines Line + 8, "End With"
            .insertlines Line + 9, "End Sub"
        End With
    Next

    'Show the form
    VBA.UserForms.Add(TempForm.Name).Show

    'Delete the form (Optional)
    'ThisWorkbook.VBProject.VBComponents.Remove TempForm

End Sub

I tend not to delete the form but build up a series of UserForms (I refer to them as TestForms) - one generated each time the code is run. In this way I can check the results of any changes I made and use the latest TestForm to manually move components, get their new positions and modify the code accordingly.

When satisfied I can rename the final TestForm to an appropriate name, add any other components such as command buttons and other event handler codes, and then delete all the remailing TestForms.

This code can also be used to create a user form on the fly but command buttons and their event handlers must be added in the code.
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top