Date query - how do I find the last April? (1 Viewer)

wilsation

Registered User.
Local time
Today, 03:02
Joined
Jul 28, 2014
Messages
44
Hi, I have several queries based around the financial year so they mostly begin in April (01/04). The following code will count all rows in April of the current year, but if I run it in January it obviously returns zero.
Code:
Count(IIf([RTB1] Between "01/04/" & Year(Date()) And "30/04/" & Year(Date()),0))
I know I can add DATE()-1 to go back to the previous year but then if you run it in May it will return results for April the previous year.
So what I need is a way of going back to the last April regardless of the current month.

Thanks
 

Ranman256

Well-known member
Local time
Yesterday, 22:02
Joined
Apr 9, 2015
Messages
4,337
Date math is done with DATEADD.
D=day
M=month
Yyyy= year

DAteAdd("yyyy",-1,"30/04/" & year(Date))
 

Galaxiom

Super Moderator
Staff member
Local time
Today, 12:02
Joined
Jan 20, 2009
Messages
12,854
Code:
DateSerial(Year(Date) + (Month(Date) < 4), 4, 1)

(Month(Date)<4) evaluates as -1 or 0 so if the month is less than April, one year is subtracted.
 
Last edited:

wilsation

Registered User.
Local time
Today, 03:02
Joined
Jul 28, 2014
Messages
44
Hi Ranman, so my code should look like this?
Code:
App1: Count(IIf([RTB1] Between DateAdd("yyyy",-1,"01/04/" & Year(Date)) And DateAdd("yyyy",-1,"30/04/" & Year(Date)),0))
When I add it to my query Access adds brackets to the Date like this...
Code:
App1: Count(IIf([RTB1] Between DateAdd("yyyy",-1,"01/04/" & Year([Date])) And DateAdd("yyyy",-1,"30/04/" & Year([Date])),0))
Which obviously doesn't work.

Thanks
 

wilsation

Registered User.
Local time
Today, 03:02
Joined
Jul 28, 2014
Messages
44
Got it sorted thanks, used this:
Code:
App1: Count(IIf([RTB1] Between DateAdd("yyyy",-1,"01/04/" & Year(Date())) And DateAdd("yyyy",-1,"30/04/" & Year(Date())),0))
 

Users who are viewing this thread

Top Bottom