How do I create a query between two dates in Access?

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.

Form Version

With the form version, I use a form, “frmReportFromQuery” to select the parameters, but the recordsource for the report is the query, “qryData”.


Fairly basic, except for the last column “Actual Close Date”

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.

VBA Version

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
End Sub

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.




By the way, if you got or are getting value from the VBA information, please give me a tip, thanks!


These posts may help answer your question too...

How To Parse A Flat File In Excel VBA

In another post I demonstrated how to access a file on your computer using the MS Office Library. Here it is if you don’t know what I’m talking about. In this post, I am going to show you how to access the file and load it into your spreadsheet. I will do the same thing […]

How can I interact with other Office applications (Excel) using VBA in Access?

Need to write your Access data or query to an Excel file? Here is the how to do it: Most people are familiar with Excel and know how to use it well (enough), and when you start talking about Access, they get scared off, and don’t know what to do anymore. Well, here you are […]

How To Create A Parameter Query In Access

A parameter query changes your ordinary static access query to be more dynamic and interactive. It will ask you a question about what you want to search for, allowing you to do a search query multiple times instead of just once. You can do your parameter query straight from the QBE (Query By Example) Editor, […]

What Is Microsoft Access Used For?

To those of you who are asking the question of “What is microsoft access used for?” , here is my opinion. I’ve been using this program for well over 15 years, and it’s become fairly easy to deal with. Many people feel that it is hard to work with, but that’s not my experience anymore […]

Previous Post

How to setup cascading comboboxes in datasheet subform

Next Post

How To Make An Access Report Based On Form Inputs