Calculating Age in Years and months in a form

J

Jessica

I'm currently creating a form, in which i have the Birth Date field. I need
to calculate the age in years and months. For example 25 years, 6 months.
What formula can i use. I tried different ones, but none of them give me the
months.
 
K

Ken Sheridan

I gave you a function for this the other day:

Public Function GetAge(varDob, Optional varDateAt)

Dim intYears As Integer, intMonths As Integer

' return date at current date if no second argument
' passed into function
If IsMissing(varDateAt) Then varDateAt = Date

If Not IsNull(varDob) Then
' get difference in years
intYears = DateDiff("yyyy", varDob, varDateAt)
' get difference in months
intMonths = DateDiff("m", varDob, _
DateSerial(Year(varDob), Month(varDateAt), Day(varDateAt)))
' adjust result if date of birth falls later in year
' than date at which age to be calculated
If varDateAt < DateSerial(Year(varDateAt), Month(varDob),
Day(varDob)) Then
intYears = intYears - 1
intMonths = intMonths + 12
End If

GetAge = intYears & " year" & _
IIf(intYears <> 1, "s", "") & ", " & _
intMonths & " month" & _
IIf(intMonths <> 1, "s", "")
End If

End Function

Paste the function into a standard module and set the ControlSource of an
unbound text box on the form to:

=GetAge([BirthDate])

Ken Sheridan
Stafford, England
 

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