VB code for calculating EXACT age from date of birth

  • Thread starter Thread starter Jeff
  • Start date Start date
J

Jeff

Can someone please help me with the VB code for
calculating an age in years automatically when a date of
birth is entered. I can get the age correct by the year,
however if the date of birth has not occurred yet for
this year the age is wrong. PLEASE HELP...

Send reply to email address
(e-mail address removed)12.in.us
 
Here is one that returns age in Years and Months. If I were born on 9/1/93
then I'e be 9y-11m now.

Ron W

Public Function GetAgeStr(varDOB As Variant, varDate As Variant) As String
' Purpose Returns a string of the age between dates in the following
format:
' Yy-Mm where Y is years and M is the months between dates
Dim dteDOB As Date, dteDate As Date
Dim lngMonths As Long, lngYears As Long
If IsDate(varDOB) And IsDate(varDate) Then
dteDOB = CDate(varDOB)
dteDate = CDate(varDate)
lngMonths = DateDiff("M", dteDOB, dteDate) Mod 12
lngYears = DateDiff("M", dteDOB, dteDate) \ 12
If DatePart("m", dteDate) = DatePart("m", dteDOB) And DatePart("d",
dteDate) < DatePart("d", dteDOB) Then
lngYears = lngYears - 1
lngMonths = 11
End If
GetAgeStr = lngYears & "y-" & lngMonths & "m"
Else
GetAgeStr = ""
End If
End Function
 
You don't need any code!

Put the following expression as the control source of an unbound control on your
form:


=DateDiff("yyyy",[DOB],Date())-IIf(Format([DOB],"mmdd")>Format(Date(),
"mmdd"),1,0)

--
PC Datasheet
A Resource for Access, Excel and Word Applications
(e-mail address removed)
www.pcdatasheet.com

· Design and basic development for new applications
· Additions, Modifications and "Fixes" for existing applications
· Mentoring for do-it-yourselfers who want guidance
· Complete application design and development
· Applications Using Palm Pilot To Collect Data And
Synchronize The Data Back To Access Or Excel
 
Back
Top