Opening and Closing Forms

VBA doesn't limit you to working with individual controls on forms. You can work with entire forms as objects, too. For example, VBA can open a closed form and display it onscreen. The OpenForm method of the DoCmd (do command) object gives you great flexibility in exactly how VBA opens a form. The syntax for using the OpenForm method of the DoCmd object is:

DoCmd.OpenForm formName, [View], [FilterName],

[WhereCondition], [DataMode], [WindowMode] [OpenArgs]

Only the first argument, formName, is required. If you omit other arguments, the form opens as it would when you just double-click the form's name in the database window, with all the property settings that are defined in the form's basic design. The optional arguments that allow you to change how the form opens are as follows:

i View: Specify how you want to open the form displayed using any of the following constants: acDesign, acFormDs (datasheet), acFormPivotChart, acFormPivotTable, acNormal (the default), acPreview.

i FilterName: If you've previously created and named a folder, use this option to filter records that the form displays. If you haven't created and named a filter, you can use the optional WhereCondition argument instead to filter records.

1 WhereCondition: Use this option to specify a record or records without using a named filter. For example, the WhereClause "[ContactID] = 1001" displays only records where the ContactID field contains 1001. The WhereClause "[State] = 'NY'" displays only records that have NY in a field named State.

1 DataMode: Determines how the form opens using the constants acFormAdd (user can add new records but not edit existing records), acFormEdit (users can add or edit data), and acFormReadOnly (users can view, but not change, data). The default argument, acFormPropertySettings, is used if you omit the argument and opens the form in normal view, honoring the AllowEdits, AllowDeleteions, AllowAddltems, and DataEntry properties defined in the form's properties.

1 WindowMode: Specifies the style of the window when opened using one of the following constants:

• acDialog (opens a dialog box with Modal and PopUp properties set to True)

• acHidden (opens the form in Form view but isn't visible onscreen)

• acIcon (opens the form minimized, leaving only its taskbar button visible)

• acWindowNormal (opens the form with setting defined in its Properties sheet).

Setting a form's Modal and PopUp properties to True makes the form open as a dialog box. When a form is modal, it must be closed before the user can perform any other action. When the PopUp property is enabled, the form stays on top of other open windows on the desktop.

1 OpenArgs: Specifies additional arguments that can be passed to the form and then processed by other code that manipulates the form.

For example, to open a form named MyForm with no special settings (as though you just double-clicked the form's name in the database window), use the simple syntax:

DoCmd.OpenForm "MyForm" The following statement opens the form named myForm in Design view:

DoCmd.OpenForm ("myForm"),acDesign

The following statement opens the form named myForm in Form view but limits the display of records to those that have (215) as the first five characters of the Phone field:

DoCmd.OpenForm ("myForm"),,,"Left(Phone,5)='(215)'"

Was this article helpful?

0 0

Post a comment