Most recent date plus one year (1 Viewer)

PeteB

Registered User.
Local time
Today, 11:21
Joined
Mar 15, 2016
Messages
78
I need to find the most recent date in a 'Date Tested' field in a table of records relating to test history, then add one year to arrive at a 'Test Due Date' and display this on the 'Testing' page (subform) of an 'Asset Details' form (whilst there are multiple past test dates, there can be only one 'Test Due Date').

As i am still at the relatively early learning stage with Access, I would appreciate help with this.
 

isladogs

MVP / VIP
Local time
Today, 19:21
Joined
Jan 14, 2017
Messages
18,211
There are many ways of doing this in Access
Here are a few to choose from - IMO number 3 is probably the simplest

dteDate represents a date e.g. dteDate = #27/01/2016# or #1/27/2016#

1. Add 365 days to date - obvious issue for leap years
Code:
dteDate+365

2. Use Access DatePart functions
Code:
DatePart("d", dteDate) & "/" & DatePart("m",dteDate) & "/" & (DatePart("yyyy",dteDate)+1)

3. Use Access Day/Month/Year functions
Code:
Day(dteDate) & "/" & Month(dteDate) & "/" & (Year(dteDate)+1)

NOTE: Methods 2 & 3 assume dd/mm/yyyy format. Change the order if your locality uses a different format

4. Create your own functions:

Code:
Public Function AddCalendarYearToDateField(strText)

If Nz(strText, "") <> "" Then
    If Format(strText, "dd/mm/") = "29/02/" Then 'modify to "28/02" to prevent leap year issues
        AddCalendarYearToDateField = "28/02/" & Format(strText, "yyyy") + 1
    Else
        AddCalendarYearToDateField = Format(strText, "dd/mm/") & Format(strText, "yyyy") + 1
    End If
End If

End Function

This function subtracts a year

Code:
Public Function SubtractCalendarYearFromDateField(strText)

If Nz(strText, "") <> "" Then
    If Format(strText, "dd/mm/") = "29/02/" Then 'modify to "28/02" to prevent leap year issues
        SubtractCalendarYearFromDateField = "28/02/" & Format(strText, "yyyy") - 1
    Else
        SubtractCalendarYearFromDateField = Format(strText, "dd/mm/") & Format(strText, "yyyy") - 1
    End If
End If

NOTE: I strongly recommend you remove spaces from all field names. If you have spaces, you need to use [] e.g. [Date Tested]
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 02:21
Joined
May 7, 2009
Messages
19,230
Add an unbound textbox on Testing subform. Put this as its recordsource:

=DateAdd("yyyy", 1, Dmax("[testing date]", "[tablename]") )
 

sonic8

AWF VIP
Local time
Today, 20:21
Joined
Oct 27, 2015
Messages
998
There are many ways of doing this in Access
Here are a few to choose from - IMO number 3 is probably the simplest

All your suggestions, except the first one, may fail depending on the Regional Date/Time settings on the executing computer.

I would go for:
DateAdd("yyyy",1,dteDate)
 

Users who are viewing this thread

Top Bottom