MS ACCESS:  Show records that display only current month and previous two months

Dale James
Dale James used Ask the Experts™
on
Hello Team

Can you please advise on the following.

I have an MS Access query which contains the following fields:

GroupID, GroupName, StartDate, EndDate, CompleteDate. Fields are pulled from a table called tblGroupBusiness.

What is required is to create a parameter of within the query which will display only three months at one time. For example:

As we are currently in the month of March, the query would only display records that have a CompleteDate from 01/01/19 through to 31/03/19.  Once we move into the month of April, all records that have a CompleteDate within the month of Jan will be hidden and the recordset would then only display all records from Feb to the end of April.

As this result can be achieved using various methods, e.g., SQL, VBA, Access functions directly into the Criteria line etc, I am happy to receive suggestions in a varied way.

Thank you in advance.

Dale
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Paul Cook-GilesSenior Application Developer

Commented:
Use the DateSerial function to calculate the first day of the month that was two months ago: DateSerial(Year(Date()), Month(Date())-2, 1), and the last day of the current month:  DateSerial(Year(Date()), Month(Date())+1, 0).

You'll need to concatenate the date delimiter # with the functions:

 where CompleteDate between #" & DateSerial(Year(Date()), Month(Date())-2, 1) & "# and #" DateSerial(Year(Date()), Month(Date())+1, 0) & "#".

I note that you're using (what I think is the) European date format (31/03/19) where an American would say 3/31/19.  I don't think that will affect the way DateSerial works, but definitely test and let us know.  :)
Dale JamesTherapist

Author

Commented:
Hello Paul

Thank you for your response and for providing the above syntax.

Yes you correct, I am using the Euro date format within this particular query.

Will apply this to the query and let you know how I get on.

Thanks again.

Dale
John TsioumprisSoftware & Systems Engineer

Commented:
SELECT GroupID, GroupName, StartDate, EndDate, CompleteDate FROM tblGroupBusiness WHERE Month(CompleteDate) between Month(DateAdd("m",-2,date())) AND Month(Date)

Open in new window

Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Most Valuable Expert 2015
Distinguished Expert 2018

Commented:
It is as simple as this - which uses true Date values  not sensitive to localised formats:

where CompleteDate between DateSerial(Year(Date()), Month(Date())-2, 1) and DateSerial(Year(Date()), Month(Date())+1, 0)"

Open in new window

Ryan ChongSoftware Team Lead

Commented:
for John's suggestion above, we need to consider the Year comparison as well, in the case we don't expect to keep one year of data only : )

in addition, we need to know if data field: CompleteDate do keep the time as well? if yes, the where clause need to be adjusted accordingly.
John TsioumprisSoftware & Systems Engineer

Commented:
Good catch Ryan..
SELECT GroupID, GroupName, StartDate, EndDate, CompleteDate FROM tblGroupBusiness WHERE (Month(CompleteDate) BETWEEN  Month(DateAdd("m",-2,date()))) AND Year(CompleteDate) BETWEEN Year(DateAdd("m",-2,date())) AND Year(Date))
Dale JamesTherapist

Author

Commented:
Hello Folks....

just want to thank everyone for their response to my original question and to confirm that all worked perfectly.

Thanks again.

Sincerely

Dale
Most Valuable Expert 2015
Distinguished Expert 2018

Commented:
You are welcome!
Then please mark as answered.

Have a nice weekend.
Software & Systems Engineer
Commented:
Well for some bizzare reason when i tried to edit my 1st post the code tags simply wouldn't work...but it seems but somehow this was for a reason
So here is my edited query to correctly take in account the case of dates in 2 different years (e.g. 2018 , 2019)
SELECT GroupID, GroupName, StartDate, EndDate, CompleteDate FROM tblGroupBusiness  
 WHERE  (iif(Month(DateAdd("m",-4,Date())) > Month(Date()),((Month([CompleteDate]) BETWEEN Month(DateAdd("m",-4,Date()))  AND 12) AND (YEAR(Todays)=Year(date())-1 )) AND ((Month(Todays) BETWEEN 1  AND Month(date())) AND (YEAR([CompleteDate])=Year([CompleteDate ]) )) ,Month([Todays]) BETWEEN Month([CompleteDate ])  AND Month(DATE() )) )

Open in new window

I raised the dif to 4 months to make it easier to spot...and also added an attached db for showing
Database141.accdb
Dale JamesTherapist

Author

Commented:
Hello John

Thank you so much for your extended post...very much appreciated!!

I placed this one in to the query and it worked perfectly.

Thank you once again.

Sincerely

Dale
Dale JamesTherapist

Author

Commented:
Once again....thank you to all of you for the time and effort provided to answer my initial question.

Sincerely

Dale

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial