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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Display Year between 2 dates 1

Status
Not open for further replies.

spartansFC

Programmer
Apr 1, 2009
165
0
0
GB
Hi

I have a table which shows a dteYear based off dteSessionDates but it's showing the wrong year, all i'm doing at the moment to get the year is:

Code:
dteYear: Format([dteSessionDates],"yyyy")

The above isn't correct and giving off some wrong dates

03/10/2023 = 2023 (which is correct)
10/01/2023 = 2023 (which is wrong, should be 2022)

An actual year is between 1 April to 31 March of the next year.

I could put a nested If statement in the query but you're only allowed so many of them, i did see a while ago where there was a table created, with the from and to dates and the year.

DateFrom Dateto Year
01/04/2020 31/03/2021 2020
01/04/2021 31/03/2022 2021
01/04/2022 31/03/2023 2022
01/04/2023 31/03/2024 2023

You add this table to the query and do a Where statement, it must be something like

Select Year from tblDateFromTo Where dteSessions is between Datefrom and Dateto

Anyone help me out

Mikie

 
You need to have a stored value of the number of months to add or subtract from the actual date to determine the calculated year. The simplest method might be to create a user defined function like:

Code:
Function GetCalcYear(datDate As Date) As Integer
    Dim intMthsAdd As Integer
    intMthsAdd = -3        [COLOR=#4E9A06]'subtract 3 months from the provided date[/color]
    GetCalcYear = Year(DateAdd("m", intMthsAdd, datDate))
    
End Function

Duane
Minnesota
Hook'D on Access
MS Access MVP 2001-2016
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top