If you run a macro containing the OpenForm action in a library database, Microsoft Access looks for the form with this name first in the library database, then in the current database.
Note The View argument setting overrides the settings of the form's DefaultView and ViewsAllowed properties. For example, if a form's ViewsAllowed property is set to Datasheet, you can still use the OpenForm action to open the form in Form view.
To open a form and restrict its records to those specified by the value of a control on another form, use the following expression:
[fieldname] = Forms![formname]![controlname on other form]
The fieldname argument is the name of a field in the underlying table or query of the form you want to open. The controlname on other form argument is the name of the control on the other form that contains the value you want records in the first form to match.
Note The maximum length of the Where Condition argument is 255 characters. If you need to enter a more complex SQL WHERE clause longer than this, use the OpenForm method of the DoCmd object in Microsoft Visual Basic instead. You can enter SQL WHERE clause statements of up to 32,768 characters in Visual Basic.
Notes
- The Data Mode argument setting overrides the settings of the form's AllowEdits, AllowDeletions, AllowAdditions, and DataEntry properties. For example, if a form's AllowEdits property is set to No, you can still use the OpenForm action to open the form in Edit mode.
- If you leave this argument blank, Access opens the form in the data entry mode set by the form's AllowEdits, AllowDeletions, AllowAdditions, and DataEntry properties.
Remarks
This action is similar to clicking the Open button or Design button after clicking Forms under Objects, and then selecting a form in the Database window.
A form can be modal (it must be closed or hidden before the user can perform any other action) or modeless (the user can move to other windows while the form is open). It can also be a pop-up form (a form used to collect or display information that remains on top of all other Access windows). You set the Modal and PopUp properties when you design the form. If you use Normal for the Window Mode argument, the form opens in the mode specified by these property settings. If you use Dialog for the Window Mode argument, these properties are both set to Yes. A form opened as hidden or as an icon returns to the mode specified by its property settings when you show or restore it.
When you open a form with the Window Mode argument set to Dialog, Access suspends the macro until the form is closed or hidden. You can hide a form by setting its Visible property to No by using the SetValue action.
The filter and WHERE condition you apply become the setting of the form's Filter property.
Examples
Set the value of a control by using a macro
The following macro opens the Add Products form from a button on the Suppliers form. It shows the use of the Echo, Close, OpenForm, SetValue, and GoToControl actions. The SetValue action sets the Supplier ID control on the Products form to the current supplier on the Suppliers form. Then the GoToControl action moves the focus to the Category ID field, where you can begin to enter data for the new product. This macro should be attached to the Add Products button on the Suppliers form.
Action | Arguments: Setting | Comment |
---|---|---|
Echo | Echo On: No | Stop screen updating while the macro is running. |
Close | Object Type: Form
Object Name: Product List Save: No |
Close Product List form. |
OpenForm | Form Name: Products
View: Form Data Mode: Add Window Mode: Normal |
Open the Products form. |
SetValue | Item: [Forms]![Products]![SupplierID]
Expression: SupplierID |
Set the Supplier ID control to the current supplier on the Suppliers form. |
GoToControl | Control Name: CategoryID | Go to the Category ID control. |
Synchronize forms by using a macro
The following macro opens a Product List form in the lower-right corner of the Suppliers form, displaying the current supplier's products. It shows the use of the Echo, MsgBox, GoToControl, StopMacro, OpenForm, and MoveSize actions. It also shows the use of a conditional expression with the MsgBox, GoToControl, and StopMacro actions. This macro should be attached to the Review Products button on the Suppliers form.
Condition | Action | Arguments: Setting | Comment |
---|---|---|---|
Echo | Echo On: No | Stop screen updating while the macro is running. | |
IsNull([SupplierID]) | MsgBox | Message: Move to the supplier record whose products you want to see, then click the Review Products button again.
Beep: Yes Type: None Title: Select a Supplier |
If there is no current supplier on the Suppliers form, display a message. |
... | GoToControl | Control Name: CompanyName | Move focus to the CompanyName control. |
... | StopMacro | Stop the macro. | |
OpenForm | Form Name: Product List
View: Datasheet Filter Name: Where Condition: [SupplierID] = [Forms]![Suppliers]![SupplierID] Data Mode: Read Only Window Mode: Normal |
Open the Product List form and show the current supplier's products. | |
MoveSize | Right: 0.7799"
Down: 1.8" |
Position the Product List form in the lower right of the Suppliers form. |