Forum Discussion
Replicating an Excel formula in Access?
Because you want the years, months and days elapsed, it needs to be done in a VBA function, rather than as a calculated value in a field.
Public Function getAge(ByVal vDateOfBirth As Variant, ByVal vDateReference As Variant) As String
Dim idays As Integer
Dim iMonths As Integer
Dim iYears As Integer
Dim iDaysInMonth As Integer
Dim dDateOfBirth As Date
Dim dDateReference As Date
dDateOfBirth = Int(Nz(vDateOfBirth, Now()))
dDateReference = Int(Nz(vDateReference, Now()))
iDaysInMonth = DateSerial(Year(dDateOfBirth), Month(dDateOfBirth) + 1, 1) - DateSerial(Year(dDateOfBirth), Month(dDateOfBirth), 1)
iYears = DatePart("yyyy", dDateReference) - DatePart("yyyy", dDateOfBirth)
iMonths = DatePart("m", dDateReference) - DatePart("m", dDateOfBirth)
idays = DatePart("d", dDateReference) - DatePart("d", dDateOfBirth)
If idays < 0 Then
iMonths = iMonths - 1
idays = iDaysInMonth + idays
End If
If iMonths < 0 Then
iYears = iYears - 1
iMonths = 12 + iMonths
End If
getAge = iYears & " yr(s). " & iMonths & " mo(s). " & idays & " day(s)"
End Function