Search a Date Range - Access 2010

webdoc

New Member
Joined
Oct 8, 2002
Messages
18
I'm using a form to run a query. I have a start date and an end date on the form and I want to only return records in the query that fall between those dates. If the dates are left blank, I want the query to return every record. The fields use the Short Date format. I would prefer to do this as part of the query design, using the Criteria field for those columns, but I'll take whatever I can get at this point. Any help is appreciated!

-Doc
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).

Redwolfx

Well-known Member
Joined
Feb 22, 2013
Messages
1,161
In the Where Clause

Code:
Where Date Between Forms![Form Name]![Starte Date box] And Forms![Form Name]![End Date box].

This will get you the info between the two dates. I'd probably write a separate query to do all records, Then use VBA like

The VBA Code is untested

Code:
Private Sub ButtonName_Click()
On Error GoTo Err_ButtonName_Click

    Dim stDocName As String
 
IF Forms![FORM Name]![Start Date Box] = "" AND Forms![FORM Name]![End Date Box] = "" Then
    stDocName = "Query1Name"
Else
    stDocName = "Query2Name"
End If
    
DoCmd.OpenQuery stDocName, acNormal, acEdit

Exit_ButtonName_Click:
    Exit Sub
Err_ButtonName_Click:
    MsgBox Err.Description
    Resume Exit_ButtonName_Click
    
End Sub
 
Last edited:
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,151,978
Messages
5,767,424
Members
425,411
Latest member
sarmientojayexce03

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top