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

Date of Birth and Year

Status
Not open for further replies.

JRWPD

Technical User
Nov 28, 2003
13
US
I am currently using the following expression which takes the entered date of Birth from the Date of Birth field and then populates the Age field with the persons age. Now I need to add the addition of populating a Year of Birth field with the year of birth.
Your suggestions on the proper code would be greatly appreaciated.
Thanks.

Private Sub DOB_BeforeUpdate(Cancel As Integer)
[Age] = DateDiff(&quot;yyyy&quot;, [DOB], Now()) + Int(Format(Now(), &quot;mmdd&quot;) < Format([DOB], &quot;mmdd&quot;))
End Sub
 
Here's how I do age. You can see it's been cobbled together by a few folks.

Function Age(varDOB As Variant, Optional varAsOf As Variant) As Integer
'Alan Browne's code, posted to cdma by Wayne Gillespie on 2001-08-02
'Modified by Jeremy Wallace 09/09/2003
'Returns whole number of years.
Dim dtDOB As Date
Dim dtAsOf As Date
Dim dtBDay As Date 'BDay in year of calculation.

'Validate parameters
If IsDate(varDOB) Then
dtDOB = varDOB
If Not IsDate(varAsOf) Then
dtAsOf = Date
Else
dtAsOf = varAsOf
End If
If dtAsOf >= dtDOB Then
dtBDay = DateSerial(Year(dtAsOf), Month(dtDOB), Day(dtDOB))
Age = DateDiff(&quot;yyyy&quot;, dtDOB, dtAsOf) + (dtBDay > dtAsOf)
End If
Else
Age = 99
End If
End Function

Jeremy

==
Jeremy Wallace
AlphaBet City Dataworks
Access Databases for Non-Profit Organizations

Please post in the appropriate forum with a descriptive subject; code and SQL, if referenced; and expected results. See thread181-473997 for more pointers.
 
Thanks a bunch. As always, this is the place to come for the answer.
JR
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top