Calculate number of years between 2 dates

  • Thread starter Thread starter Lloyd
  • Start date Start date
L

Lloyd

Can anyone help? I am trying to calculate the number of
years between 2 given dates. i.e. Age of person between
date of birth and date of death.
I have used the 'Age()' and the 'AgeMonth()' functions
created through modules from Microsoft. This however is
based on the birthdate (or any date) and the current date
function.
Thanks in advance...
 
Can you post the functions - I would have thought that you could replace the
current date variable with a date of death variable
 
Here are the functions...Thanks for responding, and so
quickly.

Option Compare Database
Option Explicit

'FUNCTION NAME: Age()
'
Function Age(varBirthdate As Variant) As Integer
Dim varAge As Variant

If IsNull(varBirthdate) Then Age = 0: Exit Function

varAge = DateDiff("yyyy", varBirthdate, Now)
If Date < DateSerial(Year(Now), Month(varBirthdate),
Day(varBirthdate)) Then
varAge = varAge - 1
End If
Age = CInt(varAge)
End Function
____________________________________________________

Option Compare Database
Option Explicit

'Function Name: AgeMonths()
'
Function AgeMonths(ByVal StartDate As String) As Integer
Dim tAge As Double
tAge = (DateDiff("m", StartDate, Now))
If (DatePart("d", StartDate) > DatePart("d", Now)) Then
tAge = tAge - 1
End If
If tAge < 0 Then
tAge = tAge + 1
End If
AgeMonths = CInt(tAge Mod 12)
End Function

__________________________________________________
 
Try

dim varDeathDate ' you will need to specify the date

and replacing Now with varDeathDate in the function

eg
Datediff(("yyyy", varBirthdate, varDeathDate)


HTH
 
One way is to modify it as follows (Note: UNTESTED CODE follows)

'FUNCTION NAME: Age()
'
Function Age(varBirthdate As Variant, varOnDate as Variant) As Integer
Dim varAge As Variant
Dim dteOnDate as Date

If IsNull(varBirthdate) Then Age = 0: Exit Function
If IsDate(varOnDate) = False then
dteOnDate = Date()
Else
dteOnDate = DateValue(varOnDate)
End if

varAge = DateDiff("yyyy", varBirthdate, dteOnDate)
If dteOnDate < DateSerial(Year(dteOnDate), _
Month(varBirthdate), _
Day(varBirthdate)) Then
varAge = varAge - 1
End If

Age = CInt(varAge)
End Function

Then call the function
Age(BirthDateField, DeathDateField)
 
Back
Top