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 on-screen. 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 Navigation pane, 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 described here:

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

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

i 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.

i 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, acFormProperty Settings, is used if you omit the argument and opens the form in Normal view, honoring the AllowEdits, AllowDeletions, AllowAddltems, and DataEntry properties defined in the form's properties.

i WindowMode: Specifies the style of the window when opened using one of these 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 on-screen

• aclcon: Opens the form minimized in the Access window

• acWindowNormal: Opens the form with settings defined in its property 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.

i 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 Navigation pane), use this 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