Coding to calculate age from a Date of Birth field...

M

maura

Can you code a field in a form to calculate a person's age for another field?
I am doing a database where the user will need to know the age of a person.
If I do a date of birth field, can this populate another field with the age
of the person?
 
D

Daniel

maura,

try this, I can't take credit for the code.

Function fAge(dteStart As Variant, dteEnd As Variant) As Variant
'*******************************************
'Purpose: Accurately return the difference
' between two dates, expressed as
' years.months.days
'Coded by: raskew (from MS Access forum)
'Inputs: From debug (immediate) window
' 1) ? fAge(#12/1/1950#, #8/31/2006#)
' 2) ? fAge(#12/30/2005#, #1/2/2006#)
'Outputs: 1) 55.8.30
' 2) 0.0.3
'*******************************************

Dim intHold As Integer
Dim dayhold As Integer

'correctly return number of whole months difference
'the (Day(dteEnd) < Day(dteStart)) is a Boolean statement
'that returns -1 if true, 0 if false
intHold = DateDiff("m", dteStart, dteEnd) + (Day(dteEnd) < Day(dteStart))

'correctly return number of days difference
If Day(dteEnd) < Day(dteStart) Then
dayhold = DateDiff("d", dteStart, DateSerial(Year(dteStart),
Month(dteStart) + 1, 0)) + Day(dteEnd)
Else
dayhold = Day(dteEnd) - Day(dteStart)
End If

fAge = LTrim(Str(intHold \ 12)) & "." & LTrim(Str(intHold Mod 12)) & "."
& LTrim(Str(dayhold))

End Function

Daniel
 
R

RobUCSD

Here's a real easy way, I have it the after update event.


Age = DateDiff("yyyy", [DOB], Now()) + _
Int(Format(Now(), "mmdd") < Format([DOB], "mmdd"))
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top