In this tip we are going to filter the results of a Report to display records with order amounts above a given value. In so doing we are going to pay special attention to two Report properties: these are FILTER, and FILTER ON LOAD.
Our solution works when the user opens a Dialog Form containing a text box and command button. The user enters an amount value in the text box, and clicks the command button. The command button then runs a small amount of VBA code to open the report. As the report loads, it applies a filter defined in the Report's Filter Property to produce the desired result.
Figure 1: Dialog Form to collect the Amount Parameter. |
So how does this work?
When the Report opens, Access checks the report's FILTER ON LOAD property (which we have set to YES). When it detects the property is set to YES, this tells Access to apply the report's Filter (which we have entered into the report's FILTER property). Our filter then references the users parameter value from the Dialog Form text box, and filters all records with Order Amounts greater than or equal to that value.
You can download the completed solution from here: Filter Property Tip Database. You may also want to work your way through the instructions below.
The Table
The solution contains a Table (tblOrders), a Dialog Form (frmParameter) and a Report (rptOrders). In the screenshot, below you will see the data we shall be working with in tblOrders:
Figure 2: tblOrders. |
As you can see the table is made up of four fields: OrderId (AutoNumber), OrderDate (Date/Time), Customer (Text), and Total (Currency).
The Dialog Form
We then create the dialog form shown in Figure 1 above (there is more information about Dialog Forms in my last post). I have referred to the textbox as txtAmount, the control button as cmdOpenReport, and the form itself as frmParameter.
As I have already mentioned, the command button runs a small amount of VBA Code when clicked by the user. To add this code:
- Select the command button on the form design grid, and click the PROPERTY SHEET icon (located in the TOOLS group of the DESIGN ribbon).
- When the PROPERTY SHEET opens, click the EVENT TAB.
- Then select the ON CLICK event by clicking into that particular cell of the grid.
- A three dot symbol appears at the right edge of the cell. Click it and select CODE BUILDER from the CHOOSE BUILDER dialog box which has opened.
- Then click the OK button and the VBA editor opens.
Figure 3: The On Click Event on the PROPERTIES SHEET. |
You can can copy the code below and paste it onto the editor. The top and bottom lines that begin Private Sub... and End Sub respectively should have appeared already, so you just need to copy the middle section in between:
Private Sub cmdOpenReport_Click()
On Error GoTo MyError
DoCmd.OpenReport "rptOrdersFiltered", acViewReport
DoCmd.Close acForm, Me.Name
Leave:
Exit Sub
MyError:
MsgBox "Error " & Err.Number & ": " & Error$
Resume Leave
End Sub
The two important lines in this section of code begin with DoCmd (meaning do command). The first of these is the command to open the form in Report View. The second closes the Dialog form once the report has opened. NB it is very important the report opens before the Dialog Form closes, because it needs to reference the value contained in the Dialog Form's Textbox as the it loads.
The Report
The quickest way to create the Report is to base it on our table.
- Select tblOrders in the Navigation Pane. It will highlight in orange.
- Then select the CREATE ribbon , and click the REPORT icon (located in the REPORTS GROUP).
This creates a basic report which we can then then modify in DESIGN VIEW as required.
Figure 4: Report Design. |
Setting the Filter Properties
Next we turn our attention to setting the Report's Filter Properties accessed via its Property sheet.
- Click the Square Box in the top right hand corner of the Report Design Grid. This ensures we are going to be working the properties of the actual Form (rather than one of its controls).
- Click on the PROPERTY SHEET icon (located in the TOOLS group of the DESIGN ribbon) whilst the Report is open in Design View.
- Click the DATA TAB of the pane.
- Make sure the SELECTION TYPE is set to REPORT. This should be the case if you carried out step 1 successfully. Otherwise just change the selection to Form in the drop down list.
- Enter the following into the FILTER property cell: Total >= forms![frmParameter]![txtAmount] . This has the effect of filtering out report records where the amount value in the Total field is greater or equal to that entered by the user in the Dialog Form's textbox.
- Then set the FILTER ON LOAD PROPERTY to YES. This ensures the filter is applied when the forms loads.
- Save the Report as rptOrdersFiltered.
- Close the Report
Figure 5: The Form's Filter Properties. Note the reference to the textbox on our Dialog Form in the FILTER property cell. |
The solution should now be ready to run. Just click the frmParameter form in the Navigation Pane to begin. The screenshot below shows what the report results look like when £50,000 is entered by the user as the amount parameter:
Figure 6: Report Results filtered for amounts greater than or equal to £50,000. |
No comments:
Post a Comment