Where do I put the code?

rgreene

Registered User.
Local time
Today, 10:01
Joined
Jan 22, 2002
Messages
168
I'm trying to calculate a persons age based on their birthdate. I did a search of this site and found several version on how to do this but I guess my question is where do I put the code? I want it so that I put in the birthdate in the bdate field and the age field is automatically calculated. so how and where do I do that?

Thanks,
Rick
 
Here is a function to do just that. It also shows the usage.

' FUNCTION NAME: Age()
'
' PURPOSE:
' Calculates age in years from a given date to today's date.
'
' INPUT PARAMETERS:
' StartDate: The beginning date (for example, a birth date).
'
' RETURN
' Age in years.
'
' Usage:
' In a query. Age:Age([FieldName])
'
' In a textbox. =Age([FieldName])
'
'*************************************************************
Function Age(varBirthDate As Variant) As Integer
Dim varAge As Variant

If IsNull(varBirthDate) Then Age = 0: Exit Function

varAge = DateDiff("yyyy", varBirthDate, Now)
If Date < DateSerial(Year(Now), Month(varBirthDate), _
Day(varBirthDate)) Then
varAge = varAge - 1
End If
Age = CInt(varAge)
End Function

In the control source of the text box that is going to show the result, put =Age([bdate])
Then when you put in a date the result will appear in the results text box.


David
 
Thanks David I'll give it a try.
I REALLY appreciate the thorough response I'm not an access expert and it's nice to get that little extra.

Thanks again,
Rick
 
Dave here is what I did (see attachment print screens). Obviously I missed something.

My attachments aren't going. Do you have an email address I can emails these to?
 
Thanks David. I emailed you and sent along my 2 print screens
 
Say your form has a birthdate field (txtBirthdate) and an age field (txtAge). In the After Update event of the birthdate field put the following code:

txtAge.value = Age(txtBirthdate.value)
 
I got the age to display the correct age. THANKS EVERYONE FOR YOUR HELP. But I noticed that it isn't saving the info it's just displaying it. How do I say after you calculate the age store those results here?
 
As a rule of thumb it's not a good idea to store calculated results in a table as they may change. Your form will happily recalculate every time, so it's not necessary to store them.
 
Tay,
I want to calculate the age of a person when he enters the shelter and I want to store that age. If the child is still in the shelter during his birthday I still want the admission form to reflect his age when he enterd the shelter not necessarily his current age.
 

Users who are viewing this thread

Back
Top Bottom