Coding dialog boxes

Custom dialog boxes can be set as either modal or modeless by using the Modal parameter of the form's Show method.

For example, the form frmFooForm can be displayed with the following code: frmFooForm.Show

Because the optional parameter Modal has, by default, the value vbModal (or 1), this code creates a modal dialog box.

If the Modal parameter is set to vbModeless (or 0), as in the following example, then a modeless dialog box is created:

frmFooForm.Show vbModeless

To open a dialog box from a macro that is available from within the application itself, you must create a public sub within a VBA module; if a sub exists within a form's code or within a class module, it cannot be made available from within the application. The sub you create cannot take any parameters.

The following example sub would launch frmFooForm as a modeless form: Public Sub showFooForm()

frmFooForm.Show vbModeless End Sub

When a form loads, it triggers its own UserForm_Initialize event. From this event handler, you should initialize all the controls on the form that must be initialized. For more information on event handlers, see "Providing event handlers in macros" on page 33.

It is possible to launch one or more other forms from within the current form by using its Show member function:

UserForm2.Show vbModal

However, VBA does not return control until all open forms have been unloaded.

0 0

Post a comment