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

How to calculate age (another method).

Access Howto:

How to calculate age (another method).

by  elizabeth  Posted    (Edited  )
The following commonly-used function fails on a small percentage of dates; the specific failure dates depend on the birth date and current date.

Function Age(Birthdate as Date) as Integer
Age = Datediff("d", Birthdate, Date) / 365.25
End Function


Instead try this:

Function Age (Birthdate as Date) as Integer
Age = DateDiff("yyyy", Birthdate, Date) + _
(Date < DateSerial(Year(Date), Month(Birthdate), Day(Birthdate)))
End Function


This takes advantage of the fact that True has a value of -1 and False has a value of 0... If this expression is True (the birthdate in the current year hasn't happened yet), you end up subtracting 1 day.

If you want to enter your own end date instead of using today's date, here is the modification:

Function Age(Birthdate As Date, Enddate as Date) As Integer
Age = DateDiff("yyyy", Birthdate, Enddate) + _
(Enddate < DateSerial (Year(Enddate), Month(Birthdate), Day(Birthdate)))
End Function


- Mostly from an article by Ken Getz in MS Office & VB Developer Mag, p.64
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top