Article ID: 100136
Article Last Modified on 1/18/2007
182568 ACC97: Microsoft Access 97 Sample Queries Available in Download Center
'==========================================================
' General Declaration
'==========================================================
Option Explicit
'*************************************************************
' FUNCTION NAME: Age()
'
' PURPOSE:
' Calculates age in years from a given date to today's date.
'
' INPUT PARAMETERS:
' StartDate: The beginning date (for example, a birth date).
'
' RETURN
' Age in years.
'*************************************************************
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
'*************************************************************
' FUNCTION NAME: AgeMonths()
'
' PURPOSE:
' Compliments the Age() function by calculating the number of months
' that have expired since the last month supplied by the given date.
' If the given date is a birthday, the function returns the number of
' months since the last birthday.
'
' INPUT PARAMETERS:
' StartDate: The beginning date (for example, a birthday).
'
' RETURN
' Months since the last birthday.
'*************************************************************
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
? Age("11/15/67")
Note that Microsoft Access responds with the value 25 (years).
? AgeMonths("11/15/67")
Note that Microsoft Access responds with the value 6, indicating that
six months have passed since this person's last birthday. Your friend
is 25 years and six months old.
=Age([OrderDate]) & " ys " & AgeMonths([OrderDate]) & " mo"NOTE: In versions 1.x and 2.0, there is a space in Order Date field name.
Additional query words: birth birthday birthdate age abc elapsed time
Keywords: kbhowto kbprogramming KB100136