[AccessD] Age calculation function

Heenan, Lambert Lambert.Heenan at AIG.com
Thu Jul 31 15:34:42 CDT 2003


Here's another solution...

Public Function CalculateAge(datBirth As Date, Optional endDate As Variant)
As Long

    Dim lngNumDays As Long
    Dim eDate As Date
    If IsMissing(endDate) Then
        eDate = Date
    Else
        eDate = endDate
    End If
    lngNumDays = DateDiff("d", datBirth, eDate)
    If Day(datBirth) = Day(eDate) And Month(datBirth) = Month(eDate) Then
        ' it's the actual birthday
        ' add 1 to compensate for .25 in the division
        lngNumDays = lngNumDays + 1
    End If
    CalculateAge = Int(lngNumDays / 365.25)
End Function

Note the second variant (date) parameter. That allows you to calculate the
age on any specific date, not just 'today'.

Lambert

> -----Original Message-----
> From:	Mark Whittinghill [SMTP:mwhittinghill at symphonyinfo.com]
> Sent:	Thursday, July 31, 2003 3:52 PM
> To:	accessd at databaseadvisors.com
> Subject:	[AccessD] Age calculation function
> 
> Hi all,
> 
>   I need an age calculation function to calculate current age from
> birthday.
> If the person's 30th birthday is on August 1, I want them to be 29 on
> July31st.  DateDiff rounds up and makes them 30.  I made this function
> 
> Public Function CalculateAge(datBirth As Date) As Long
> 
>     Dim lngNumDays As Long
>     lngNumDays = DateDiff("d", datBirth, Date)
> 
>     CalculateAge = Int(lngNumDays / 365.25)
> 
> End Function
> 
> 
> It works, except when today's date is their birthday.  In my example, on
> their birthday, the function returns 29.  The next day it works.  It works
> for some years on the birthday and not others.  How can I make this
> foolproof?
> 
> Mark Whittinghill
> Symphony Information Services
> 612-333-1311
> mwhittinghill at symphonyinfo.com
> 
> 
> _______________________________________________
> AccessD mailing list
> AccessD at databaseadvisors.com
> http://databaseadvisors.com/mailman/listinfo/accessd
> Website: http://www.databaseadvisors.com


More information about the AccessD mailing list