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

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
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:
Upvote 0

Forum statistics

Threads
1,213,515
Messages
6,114,080
Members
448,548
Latest member
harryls

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
Back
Top