Creating and Adding Controls to a Dialog Box

A dialog box can be used to get input from a user and then use the values received for the subroutines defined in the project.

A UserForm object is used to represent a dialog box in a VBA project. After a UserForm object has been added to a project, you can use the tools on the Toolbox window to add controls to and select controls on a UserForm object. Once a control tool is clicked, click and drag over the UserForm object to size and position the control. Controls on a UserForm object can be a text box, drop-down list, radio optional buttons, and command buttons among many others. Command buttons are typically used to represent the OK and Cancel buttons of a dialog box.

In the following steps, you learn to insert a UserForm object into the GardenPath project and then add controls to it.

  1. In the VBA IDE, on the menu bar, click Insert User Form to insert a new form into the project.

    Two windows are displayed; the Toolbox window and a form editor window for the new UserForm object.

  2. On the Toolbox window, click the OptionButton () tool.
  3. In the form editor window, click and drag to place the option button control as shown in the following illustration.

  4. Click the OptionButton () tool again and click below the first option button control you placed. Click do not drag.

    A second option button control is placed and at a default size.

  5. Continue placing label, text box, and command button controls as shown in the previous illustration.

    You should place

    • Three labels ()
    • Three text boxes ()
    • Two command buttons ()
  6. On the Toolbox window, click the Select Objects tool.
  7. In the form editor window, select a control to reposition and resize it. Do any of the following:
    • Click and drag the control to reposition it.
    • Click and drag a grip handle on the control to resize it.
  8. Click on the UserForm object or press Esc to clear the selection of the control.
  9. Continue repositioning and resizing the controls on the UserForm object.
  10. Save the project.