You are on page 1of 7

Step 1: Create an unbound form that prompts for report parameters

1. On the Create tab, in the Forms group, click Form Design. 2. In Design view, press F4 to display the property sheet and then set the form properties, as shown in the following table.
Property Caption Default View Allow Form View Allow Datasheet View Allow PivotTable View Allow PivotChart View Scroll Bars Record Selectors Navigation Buttons Border Style Setting Enter the name you want to appear in the title bar of the form. Single Form Yes No No No Neither No No Dialog

3. For each parameter that you want the form to collect, click Text Box in the Controls group on the Design tab. 4. Set the properties for the text boxes, as shown in the following table.
Property Name Format Setting Enter a name that describes the parameter, for example, BeginningDate. Choose a format that reflects the data type of the parameter field. For example, select Medium Date for a date field.

5. Save the form and give it a name, such as Parameter Collector.

NOTE

This exercise uses Parameter Collector as the name of the form. As you follow

these instructions, substitute the name that you gave to your report parameter form wherever these instructions use the name Parameter Collector.

Step 2: Create a module containing a function that checks whether a form is already loaded
1. On the Create tab, in the Other group, click Module. If this command is unavailable, click the arrow beneath either the Macro or the Class Module button, and then click Module. A new module opens in the Visual Basic Editor. 2. Type or paste the following code into the Visual Basic Editor: 3. Function IsLoaded(ByVal strFormName As String) As Boolean 4. 5. Dim oAccessObject As AccessObject 6. Set oAccessObject = CurrentProject.AllForms(strFormName) 7. 8. If oAccessObject.IsLoaded Then 9. If oAccessObject.CurrentView <> acCurViewDesign Then 10. IsLoaded = True 11. End If 12. End If

Step 3: Create a macro group that controls the Parameter Collector form
1. On the Create tab, in the Other group, click Macro. If this command is unavailable, click the arrow beneath either the Module or the Class Module button, and then click Macro. 2. On the Design tab, in the Show/Hide group, click Show All Actions. 3. On the Design tab, in the Show/Hide group, click Macro Names to display the Macro Name column. 4. Type a macro name, such as Open Dialog, in the Macro Name column.

5. Click the cell in the Action column next to the new macro name, click the drop-down arrow, and then click OpenForm. 6. In the Action Arguments grid (located below the macro design grid), set the action arguments (action argument: Additional information required by some macro actions. For example, the object affected by the action or special conditions under which the action is carried out.), as shown in the following table.
Argument Form Name Setting Enter the name you gave your report parameter form. (Parameter collector) Form Edit Dialog

View Data Mode Window Mode

7. In the next Action cell, click CancelEvent to add a second action. This action will be used to cancel previewing or printing the report when a user clicks the Cancel button on the form. 8. If the Condition column is not displayed, click Conditions in the Show/Hide group on the Design tab. 9. In the Condition column, type Not IsLoaded, followed by an opening parenthesis and a quotation mark, followed by the name you gave your report parameter form, and then followed by a quotation mark and a closing parenthesis. For example, if your form is named Parameter Collector, you type Not IsLoaded("Parameter Collector"). 10. In the next blank row of the macro design grid, type a new macro name, such as Close Dialog. 11. In the adjacent Action cell, click the drop-down arrow and then click Close. This action will be used by the report to close the report parameter form. Set its action arguments, as shown in the following table.

Argument Object Type Object Name

Setting Form Enter the name you gave your report parameter form. (parameter Collector) No

Save

12. In the next blank row of the macro design grid, type a new macro name, such as OK. In the adjacent Action cell, click the drop-down arrow, and then click SetValue. This action will be used to hide the report parameter form when the user clicks OK. Set its action arguments, as shown in the following table.
Argument Item Expression Setting [Visible] No

13. In the next blank row of the macro design grid, type a new macro name, such as Cancel. In the adjacent Action cell, click the drop-down arrow, and then click Close. This action will be used to close the report parameter form when the user clicks Cancel. Set its action arguments, as shown in the following table.
Argument Object Type Object Name Save Setting Form Enter the name you gave your report parameter form. No

14. Save and close the macro group. Give the macro group a name, for example, Parameter Collector.

Step 4: Add OK and Cancel command buttons to the form

1. Reopen the Parameter Collector form in Design view. 2. Ensure that Use Control Wizards in the Controls group on the Design tab is not selected. 3. On the Design tab, in the Controls group, click Button. 4. Position the pointer below the text boxes on your form, and then drag to create an OK command button. 5. If the property sheet isn't visible, press F4 to display it. 6. Set the OK button's properties, as shown in the following table.

Property Name Caption Default OnClick

Setting OK OK Yes Enter the name of the macro, for example, Parameter Collector.OK.

7. Create a Cancel command button and set its properties, as shown in the following table.
Property Name Caption OnClick Setting Cancel Cancel Enter the name of the macro, for example, Parameter Collector.Cancel.

8. Save and close the form.

Step 5: Enter the criteria in the underlying query or stored procedure for the report
1. Open the underlying query or stored procedure for the report in Design view. 2. Enter the criteria for the data. In the expression (expression: Any combination of mathematical or logical operators, constants, functions, and names of fields, controls, and properties that evaluates to a single value. Expressions can perform calculations, manipulate characters, or test data.), use the Forms object, the name of the form, and the names of the controls (control: A graphical user interface object, such as a text box, check box, scroll bar, or command button, that lets users control the program. You use controls to display data or choices, perform an action, or make the user interface easier to read.) in the criteria:


For example, in an Access database (.accdb or .mdb), for a form named Parameter Collector, you use the following expression to refer to controls named Beginning Date and Ending Date in the query: Between [Forms]![Parameter Collector]![Beginning Date] And [Forms]![Parameter Collector]![Ending Date]

In an Access project (Microsoft Access project: An Access file that connects to a Microsoft SQL Server database and is used to create client/server applications. A project file doesn't contain any data or data-definition-based objects such as tables and views.) (.adp), you must first explicitly name the parameters in the stored procedure, for example: @Beginning_Date datetime, @Ending_Date datetime You then use those parameters in the WHERE clause, for example: WHERE Sales.ShippedDate Between @Beginning_Date And @Ending_Date In an Access project, you set the reference to the controls on the form in the InputParameters property in the report, as shown in the next procedure.

Step 6: In an Access project, set the InputParameters property in the main report
1. Open the report in Design view. 2. Set the report's InputParameters property to a string that specifies the parameters that are passed to the stored procedure that the report is bound to. As in the following example, the string must be an expression that includes the parameters that you specified in the stored procedure and also the reference to the controls in the dialog box: @Beginning_date datetime = [Forms]![Parameter Collector]![Beginning Date], @Ending_date datetime = [Forms]![Parameter Collector]![Ending Date]

Step 7: Attach the macros to the main report


1. Open the report in Design view. 2. If the property sheet is not visible, press F4 to display it. 3. Set the report properties, as shown in the following table.
Property OnOpen Setting Type the name of the macro that opens the form, for example, Parameter Collector.Open Dialog. Type the name of the macro that closes the form, for example, Parameter Collector.Close Dialog.

OnClose

You might also like