Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Access Query Last Year Month Total 1

Status
Not open for further replies.

dgp2

Technical User
Nov 5, 2002
14
US
Could someone please help. I am trying to write a query that will pull all the orders for the current month LAST YEAR. For instance since this is February 2003 I would want all the orders for February of 2002. I would like this query to be automated so I don't have to put in the new parameters every month. The field name in my table is named DateEntered and its data type is a date field. Any help would be appreciated.
 
Hiya

Try:
Code:
>=DateSerial(Year(Now())-1,Month(Now()),1) And 
<=DateSerial(Year(Now())-1,Month(Now()),IIf(Month(Now())=1 Or Month(Now())=3 Or Month(Now())=5 Or Month(Now())=7 Or Month(Now())=8 Or Month(Now())=10 Or Month(Now())=12,31,IIf(Month(Now())=2,28,30)))
This uses Access SQL functions & was created in the Design window in Access 2000

If you want it to take leap years into account, change the 28 in the last part of the function to another IIF to test if Year(Now()) is divisible by 4

HTH!

Cheers
Nikki
 
Thank you very, very much. I can't tell you how much time I've spent on this.

I don't understand how to check for the leap year, can you explain this?

Thanks again.
 
Yup. You need to test whether the year's divisible by 4 - now I don't think Access has the MOD function like Excel (this returns the rest left by a division, rather than the result of the division), but you could simulate that with this:

Code:
IIF( (Year(Now()-1) / 4 - INT((Year(Now()-1) / 4) = 0 , 29, 28)

You're testing whether dividing by 4 leaves no rest. If it doesn't you have a leap year and therefore the month of feb has 29 days.

HTH!

Cheers
Nikki
 
Thanks for explaining. I really appreciate all your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top