Query that changes criteria based on current date

RogerC

Well-known Member
Joined
Mar 25, 2002
Messages
536
I have a query that includes a Date field. The criteria for 'Date' is a range of 28 days (for example 'Between 7/27/2003 And 8/23/2003'). Periodically, I need the Date's criteria to automatically change based on the current date. For example; if today's date is within 7/27/2003 and 8/23/2003 I want the 'Date' field to use the criteria 'Between 7/27/2003 and 8/23/2003'. On 8/24/2003 I need the criteria to automatically change to 'Between 8/24/2003 and 9/20/2003'. On 9/21/2003 it would change again, etc. Can this be done in Access?
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
You can use the Date() function to achieve this. But watch out with your field naming -- calling a field Date can confuse Access because it may think you are referring to its internal function -- best to call it SalesDate or something appropriate. Anyway...
In the criteria line for your field, put this:
Code:
Between Date()-28 And Date()
That will give you the last 4 weeks' data, updating as the date changes.

HTH
Denis
 
Upvote 0
If you want what I think you want then try this for your criteria.
Code:
>Date()-(((Date()/28)-Int(Date()/28))*28) And <Date()-(((Date()/28)-Int(Date()/28))*28)+29

Not very elegant but it should work :-)
 
Upvote 0
Oops. I'm afraid I wasn't clear enough in describing my problem. 28 days was just an example of the time period for one partictular month. The length of any particular time period varies. The next month may be 35 days, etc. These repesent the the 'financial closing calendar' for our company. I have a list of the start and stop dates for each month. Can I build the start and stop dates for all 12 months into a formula for the Date criteria?

Thanks for the tip SydneyGeek. I will change the name of the Date field to MonthCloseDate.
 
Upvote 0
You will probably be best building a UDF to check the dates. it will be easier to maintain than trying to manage a large iif statement.



Peter
 
Upvote 0
thanks bat17. Unfortunately, I'm new to Access and wouldn't know where to begin with a UDF. Can you give me any ideas on how to get started?
 
Upvote 0
Paste this code into a new module and save the module with any name that is not the same as the functions (DateFilter).

Code:
Function DateFilter(fStart As Boolean) As Date
'True will return Start Date, False will return End Date
Dim StartDate As Date
Dim endDate As Date

Select Case DATE
Case #1/1/2003# To #1/31/2003#
   StartDate = #1/1/2003#
   endDate = #1/31/2003#
Case #2/1/2003# To #2/28/2003#
   StartDate = #2/1/2003#
   endDate = #2/28/2003#
Case #3/1/2003# To #3/31/2003#
   StartDate = #3/1/2003#
   endDate = #3/31/2003#
Case #4/1/2003# To #4/30/2003#
   StartDate = #4/1/2003#
   endDate = #4/30/2003#
Case #5/1/2003# To #5/31/2003#
   StartDate = #5/1/2003#
   endDate = #5/31/2003#
Case #6/1/2003# To #6/30/2003#
   StartDate = #6/1/2003#
   endDate = #6/30/2003#
Case #7/1/2003# To #7/31/2003#
   StartDate = #7/1/2003#
   endDate = #7/31/2003#
Case #8/1/2003# To #7/31/2003#
   StartDate = #8/1/2003#
   endDate = #7/31/2003#
Case #9/1/2003# To #9/30/2003#
   StartDate = #9/1/2003#
   endDate = #9/30/2003#
Case #10/1/2003# To #10/31/2003#
   StartDate = #10/1/2003#
   endDate = #10/31/2003#
Case #11/1/2003# To #11/30/2003#
   StartDate = #11/1/2003#
   endDate = #11/30/2003#
Case #12/1/2003# To #12/31/2003#
   StartDate = #12/1/2003#
   endDate = #12/31/2003#
End Select
If fStart = True Then
   DateFilter = StartDate
Else
   DateFilter = endDate
End If
End Function

In the criteria for the date field in the query put
Code:
>=DateFilter(True) And <=DateFilter(False)

I have set it up to filter against the months of the year but you can change the dates to suit your needs. I have not added any error trapping for things like invalid dates!

HTH

Peter
 
Upvote 0
bat17 - That works perfectly! ...and now I know how to create a UDF! Thanks for your help! :-D
 
Upvote 0

Forum statistics

Threads
1,223,560
Messages
6,173,030
Members
452,500
Latest member
jesuisazep

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