Components of the User Form Object

In the same manner as ActiveX controls, when you select a form, its properties appear in the Properties window in the VBA IDE (see Figure 6.3). Table 6.1 lists some of the properties of the UserForm object that you will commonly set at Design Time.

Table 6.1 Selected Properties of the UserForm Object

/ \ Property Description

Name Sets the name of the UserForm object for use as a code reference to the object.

BackColor Sets the background color of the form.

Caption Sets the text displayed in the title bar.

Height Sets the height of the form.

StartUpPosition Sets the position of the form on the screen when displayed.

Width Sets the width of the form.

The UserForm object has several additional appearance properties besides those Listed in Table 6.1. These properties include BorderColor, BorderStyle, and SpecialEffect which are used for aesthetic appeal.

Forms represent separate entities in a VBA project and have their own code window. To view the code window (module) associated with the UserForm object, select the View Code icon from the Project Explorer; or select View, Code from the menu bar; or hit F7 (all with the form selected). You can also double click on the form to open its code window. The structure of a form code window (sometimes referred to as a form module) is the same as any other module window. The upper-left corner contains a dropdown list with all objects contained within the form, including the UserForm object. The upper-right corner contains a dropdown list of all event procedures associated with the various objects that may be contained in the form. There is also a general declarations section for making module level declarations in the form module. An example code window for a form is shown in Figure 6.4.

Event procedure -dropdown list

Object -dropdown list

The code window of a form-otherwise known as a form module.

Event procedure -dropdown list

Object -dropdown list

The code window of a form-otherwise known as a form module.

The behavior of variables and procedures declared with the Dim, Private, and Public keywords in a form module are identical to that of an object module as discussed in Chapter 3. Thus, the scope of variables and procedures declared as Public in the general declarations section of a form module are global, but must be accessed from other modules using the variable's module identifier (for example, moduleName.variableName or moduleName.procedureName).

The UserForm object has several event procedures, including Click(), Activate(), and QueryClose() among others. To view the full list of event procedures of the UserForm object, select the UserForm object in the object dropdown list and then select the event procedure dropdown list from the form module (see Figure 6.4). Some of these event procedures should be familiar, as they are common to several ActiveX controls. Table 6.2 lists a few of the more commonly used event procedures of the UserForm object.

Event

Activate() Initialize() QueryClose() Terminate()

Description

Triggered when the UserForm is activated (i.e., shown). Triggered when the UserForm is loaded. Triggered when the UserForm is closed or unloaded. Triggered when the UserForm is closed or unloaded.

Event

Activate() Initialize() QueryClose() Terminate()

Description

Triggered when the UserForm is activated (i.e., shown). Triggered when the UserForm is loaded. Triggered when the UserForm is closed or unloaded. Triggered when the UserForm is closed or unloaded.

The Initialize() event of the UserForm object is triggered when the form is first loaded, and therefore, is an excellent location for code that initializes program variables and controls. The Activate() event is also used for initialization; however, it is not triggered when the UserForm object is loaded, but only when the form is made active. The QueryClose() and Terminate() events are triggered when the UserForm is unloaded from memory, making these event procedures good locations for code that clears memory and/or ends the program.

Biorhythm Awareness

Biorhythm Awareness

Who else wants to take advantage of biorhythm awareness to avoid premature death, escape life threatening diseases, eliminate most of your life altering mistakes and banish catastrophic events from your life.

Get My Free Ebook


Post a comment