Creating Forms

Although ready to use and convenient, the built-in dialog boxes will not meet all of your VBA application's requirements. Apart from displaying a dialog box on the screen and specifying its initial settings, you can't control the dialog box's appearance. You can't decide which buttons to add, which ones to remove, and which ones to move around. Also, you can't change the size of the built-in dialog box. If you're looking to provide a custom interface, your only solution is to create a user form.

A user form is like a custom dialog box. You can add various controls to the form, set properties for these controls, and write VBA procedures that respond to form and control events. Forms are separate objects that you add to your VBA project by choosing Insert | UserForm from the Visual Basic Editor menu.

Forms can be shared across applications. For example, you can reuse the form you designed in Microsoft Excel in Microsoft Word or any other application that uses Visual Basic Editor.

To create a custom form, follow these steps:

■ Switch to the Visual Basic Editor window

■ Choose Insert | UserForm

A new folder called Forms appears in the Project Explorer window. This folder contains a blank UserForm. The work area automatically displays the form and the Toolbox with the necessary tools for adding controls.

Figure 10-7:

A new form can be added to the open VBA project by selecting UserForm from the Insert menu.

Figure 10-7:

A new form can be added to the open VBA project by selecting UserForm from the Insert menu.

The Properties window (Figure 10-8) displays a number of properties that you can set, depending on your needs. The form properties are arranged into seven categories: Appearance, Behavior, Font, Misc, Picture, Position, and Scrolling. To list form properties by category, click the Categorized tab in the Properties window. To find out information about a specific property, click the property name and press F1. The online help will be launched with the property description topic.

After adding a new form to your VBA project, you should assign a unique name to this form by setting the Name property. In addition to the name, each form should contain a title. You can set the title for your form by using the Caption property.

Was this article helpful?

0 0

Post a comment