Rmaster2022
Member
- Local time
- Today, 07:34
- Joined
- Apr 1, 2022
- Messages
- 32
I copied the following function from a website but edited it to reflect the name of the birth date field:
On the form I have a field 'birthdate" and next to it another text box that will show the age. How do I reference this function in the form text box and/or the query so when one opens the form, the age appears?
Code:
Public Function AgeYears(ByVal BirthDate As Date) As Integer
' Comments: Returns the age in years
' Params : BirthDate Date to check
' Returns : Number of years
' Source : Total Visual SourceBook
On Error GoTo PROC_ERR
Dim intYears As Integer
intYears = Year(Now) - Year(BirthDate)
If DateSerial(Year(Now), Month(BirthDate), Day(BirthDate)) > Now Then
' Subtract a year if birthday hasn't arrived this year
intYears = intYears - 1
End If
AgeYears = intYears
PROC_EXIT:
Exit Function
PROC_ERR:
MsgBox "Error: " & Err.Number & ". " & Err.Description, , "modDateTime.AgeYears"
Resume PROC_EXIT
End Function
On the form I have a field 'birthdate" and next to it another text box that will show the age. How do I reference this function in the form text box and/or the query so when one opens the form, the age appears?
Last edited by a moderator: