<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<META content="MSHTML 6.00.2600.0" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=#ffffff>
<DIV>
<DIV><STRONG>Gustav,</STRONG></DIV>
<DIV><STRONG></STRONG> </DIV>
<DIV> Threads related to age calculation appear to remain ever
popular.</DIV>
<DIV> </DIV>
<DIV> Any reason for limiting the return value to an
integer ? Would it not be preferable to have it as single (Integer portion to
represent comnpleted years and decimal portion based upon number of days lapsed
in final unfinished year if any) ? The result could be used at
the level of precision actually needed.</DIV>
<DIV> </DIV>
<DIV>Regards,</DIV>
<DIV><STRONG>A.D.Tejpal</STRONG></DIV>
<DIV><STRONG>--------------</STRONG></DIV>
<DIV><STRONG></STRONG><BR>----- Original Message ----- <BR>From: "Gustav Brock"
<<A href="mailto:gustav@cactus.dk">gustav@cactus.dk</A>><BR>To: "Access
Developers discussion and problem solving"<BR><<A
href="mailto:accessd@databaseadvisors.com">accessd@databaseadvisors.com</A>><BR>Sent:
Saturday, August 02, 2003 4:24 AM<BR>Subject: Re: [AccessD] Age calculation
function<BR><BR>> Hi Mark<BR>><BR>> > Is there a solution to
this?<BR>><BR>> Certainly. It can be found here at Drew's
archive:<BR>><BR>> <A
href="http://www.wolfwares.com/AccessD/postinfo.asp?Post=29448">http://www.wolfwares.com/AccessD/postinfo.asp?Post=29448</A><BR>><BR>>
However, Drew, the indenting is bad, so below is again the function we<BR>>
use.<BR>><BR>> I'm not claiming this method to be the mother of anything
but it works<BR>> without errors. To quote myself from
2002-10-04:<BR>><BR>> <quote><BR>><BR>> We have have had the
need to calculate the correct age no matter what,<BR>> and I have yet to see
this function fail. It has been posted before but<BR>> with so many
"Age_is_just_about()" functions it seems to be the time to<BR>> post it
again.<BR>><BR>> The crucial point is the correct age count for those poor
souls born on<BR>> the 29th of February:<BR>><BR>><BR>> Function
Age(ByVal datDateOfBirth As Date, Optional ByVal varDate As<BR>Variant) As
Integer<BR>><BR>> ' Calculates age at today's date or at a specified date
earlier or later<BR>in time.<BR>> ' Uses Years() for calculating difference
in years.<BR>> '<BR>> ' Gustav Brock, Cactus Data ApS.<BR>> '
2000-11-03.<BR>><BR>> Dim datDate As
Date<BR>><BR>> ' No special error
handling.<BR>> On Error Resume Next<BR>><BR>>
If IsDate(varDate) Then<BR>> datDate =
CDate(varDate)<BR>> Else<BR>> datDate
= Date<BR>> End If<BR>><BR>> Age =
Years(datDateOfBirth, datDate)<BR>><BR>> End Function<BR>><BR>>
Function Years(ByVal datDate1 As Date, ByVal datDate2 As Date) As
Integer<BR>><BR>> ' Returns the difference in full years between datDate1
and datDate2.<BR>> '<BR>> ' Calculates correctly for:<BR>>
' negative differences<BR>> ' leap years<BR>>
' dates of 29. February<BR>> ' date/time values with
embedded time values<BR>> ' negative date/time values (prior to
1899-12-29)<BR>> '<BR>> ' Gustav Brock, Cactus Data ApS.<BR>> '
2000-11-03.<BR>> ' 2000-12-16. Leap year correction modified to be
symmetrical.<BR>>
'
Calculation of intDaysDiff simplified.<BR>>
'
Renamed from YearsDiff() to Years().<BR>> ' 2000-12-18. Added
cbytMonthDaysMax.<BR>><BR>> ' Constants for leap year
calculation. Last normal date of February.<BR>> Const
cbytFebMonth As Byte = 2<BR>> Const
cbytFebLastDay As Byte = 28<BR>> ' Maximum
number of days in a month.<BR>> Const cbytMonthDaysMax As
Byte = 31<BR>><BR>> Dim
intYears As
Integer<BR>> Dim
intDaysDiff As
Integer<BR>> Dim
intReversed As
Integer<BR>><BR>> ' No special error
handling.<BR>> On Error Resume Next<BR>><BR>>
intYears = DateDiff("yyyy", datDate1, datDate2)<BR>> If intYears
= 0 Then<BR>> ' Both dates fall within the same
year.<BR>> Else<BR>> ' Check for
ultimo February and leap years.<BR>> If
(Month(datDate1) = cbytFebMonth) And (Month(datDate2) =<BR>cbytFebMonth)
Then<BR>> ' Both dates fall in
February.<BR>> ' Check if dates are at
ultimo February.<BR>> If (Day(datDate1)
>= cbytFebLastDay) And (Day(datDate2) >=<BR>cbytFebLastDay)
Then<BR>> ' Both dates are at
ultimo February.<BR>> ' Check
if the dates fall in leap
years.<BR>> If
Day(DateSerial(Year(datDate1), cbytFebMonth + 1, 0)) =<BR>cbytFebLastDay Xor
_<BR>>
Day(DateSerial(Year(datDate2), cbytFebMonth + 1, 0)) =<BR>cbytFebLastDay
Then<BR>> ' Only
one date falls within a leap
year.<BR>> '
Adjust both dates to day 28 of
February.<BR>>
datDate1 = DateAdd("d", cbytFebLastDay -
Day(datDate1),<BR>datDate1)<BR>>
datDate2 = DateAdd("d", cbytFebLastDay -
Day(datDate2),<BR>datDate2)<BR>>
Else<BR>> ' Both
dates fall either in leap years or non leap
years.<BR>> ' No
adjustment needed.<BR>> End
If<BR>> End
If<BR>> End If<BR>> '
Calculate day difference using months and days as Days() will
fail<BR>when<BR>> ' comparing leap years with non
leap years for dates after February.<BR>> intDaysDiff
= (Month(datDate1) * cbytMonthDaysMax + Day(datDate1)) -<BR>(Month(datDate2) *
cbytMonthDaysMax + Day(datDate2))<BR>> intReversed =
Sgn(intYears)<BR>> ' Decrease count of years by one
if dates are closer than one year.<BR>> intYears =
intYears + (intReversed * ((intReversed * intDaysDiff)
><BR>0))<BR>> End If<BR>><BR>> Years =
intYears<BR>><BR>> End Function<BR>><BR>>
</quote><BR>><BR>> /gustav<BR></DIV></DIV></BODY></HTML>