This post will show you how to create a query between two dates in Access using
a query and in VBA.
We will also show the results of the query in a report format, like shown in the image.
The report dates are the selections from the parameter form, and can be accomplished with both
methods of producing the report.
Both methods of this parameter query are selected in the same way.
This is the form used to select the dates for the report.
How do I create a button to run a query in Access?
When you click the button “Run Report”, you’ll run the query which opens the report.
With the form version, I use a form, “frmReportFromQuery” to select the parameters, but the recordsource for the report is the query, “qryData”.
Between [Forms]![frmReportFromQuery]![cboStartDate] And [Forms]![frmReportFromQuery]![cboEndDate]
This syntax selects just the “Actual Close Date” between the 2 comboboxes in the form.
When you use “frmReportFromQuery”, the query is fired based on the 2 dates and the report, “rptDataFromForm” is displayed.
With the VBA version, I use a similar form, “frmReportFromVBA” to select the parameters, but the recordsource for the report is the table, “tblData”.
The only thing that is different on the report is in the Report_Open event I place the following code:
Private Sub Report_Open(Cancel As Integer)
Dim strSQL As String
Dim strWhere As String
strSQL = "SELECT * FROM tblData WHERE "
strWhere = "[Actual Close Date] Between #" & Forms("frmReportFromVBA").Controls("cboStartDate") & "# And #" & Forms("frmReportFromVBA").Controls("cboEndDate") & "#"
Me.RecordSource = strSQL & " " & strWhere
IHere is the database: How do I create a query between two dates in Access.accdb
Watch how it’s done:
Let me know if you have any questions.