Provided by Allen Browne, allen@allenbrowne.com
Here are two methods to limit the records in a report to a user-specified range of dates.
For a more comprehensive example that combines other criteria with the dates, see Search Criteria database.
The simplest approach is to base the report on a parameter query. This approach works for all kinds of queries, but has these disadvantages:
To create the parameter query:
Between [StartDate] And [EndDate]
StartDate Date/Time
EndDate Date/Time(Note: Step 3 is optional, but strongly recommended. It prevents invalid dates being entered, and helps Access understand the date regardless of your regional setting date format.)
The alternative is to use a small unbound form where the user can enter the limiting dates. This approach may not work if the query aggregates data, but has these advantages:
Here are the steps. This example assumes a report named rptSales, limited by values in the SaleDate field.
Dim strReport As String 'Name of report to open. Dim strField As String 'Name of your date field. Dim strWhere As String 'Where condition for OpenReport. Const conDateFormat = "\#mm\/dd\/yyyy\#" strReport = "rptSales" strField = "SaleDate" If IsNull(Me.txtStartDate) Then If Not IsNull(Me.txtEndDate) Then 'End date, but no start. strWhere = strField & " <= " & Format(Me.txtEndDate, conDateFormat) End If Else If IsNull(Me.txtEndDate) Then 'Start date, but no End. strWhere = strField & " >= " & Format(Me.txtStartDate, conDateFormat) Else 'Both start and end dates. strWhere = strField & " Between " & Format(Me.txtStartDate, conDateFormat) _ & " And " & Format(Me.txtEndDate, conDateFormat) End If End If ' Debug.Print strWhere 'For debugging purposes only. DoCmd.OpenReport strReport, acViewPreview, , strWhere
Now when you click the Ok button, the filtering works like this:
You will end up using this form for all sorts of reports. You may add an option group or list box that selects which report you want printed, and a check box that determines whether the report should be opened in preview mode.
| Home | Index of tips | Top |