"DP" <DP@hotmail.com> wrote in message
news:D6*****************@newsfe6-gui.ntli.net...
hi,
i've got a customer table, with a date of birth field in it. its entered
like; 12/12/1992
how would i generate an age through this field.
e.g. i've got txtAge.
how would i have the correct age inserted in there?
thanx
dev
cut and paste the function into a new module. Then you could call it by
setting the control source of a textbox to
=GetAge([MyDateOfBirthField])
Public Function GetAge(BirthDate As Variant) As String
On Error GoTo Err_Handler
Dim dteDOB As Date
Dim lngDays As Long
Dim lngMonths As Long
Dim lngYears As Long
Dim strAge As String
strAge = "Error!"
If Not IsNull(BirthDate) Then
dteDOB = CDate(BirthDate)
lngMonths = DateDiff("m", dteDOB, Date)
lngDays = DateDiff("d", DateAdd("m", lngMonths, dteDOB), Date)
If lngDays < 0 Then
lngMonths = lngMonths - 1
lngDays = DateDiff("d", DateAdd("m", lngMonths, dteDOB), Date)
End If
lngYears = lngMonths \ 12
lngMonths = lngMonths Mod 12
strAge = CStr(lngYears) & " yrs " & _
CStr(lngMonths) & " mths " & _
CStr(lngDays) & " days "
Else
strAge = "Unknown"
End If
Exit_Handler:
GetAge = strAge
Exit Function
Err_Handler:
Resume Exit_Handler
End Function