Getting the age to display from the birthdate

S

sea_minerals

Is there a way to get the age of a client to appear next to the
birthdate?

I have a form, that people will fill in - once they filled in the
birthdate, is it possible to have a box next to it that will display
their age?

Im assuming that i'll need a code or something, where does that
usually go?


thanks for any help.
 
G

Guest

Place the following code in a standard Module:
Function dhAge(dtmBD As Date, Optional dtmDate As Date = 0) As Integer
'Calculate a person's age, given the person's birth date and an
'optional "current" date
If dtmDate = 0 Then
'Did the caller pass in a date? If not, use the current date.
dtmDate = Date
End If
dhAge = DateDiff("yyyy", dtmBD, dtmDate) + (dtmDate <
DateSerial(Year(dtmDate), _
Month(dtmBD), Day(dtmBD)))
End Function

Be aware of wraping in the code as posted.

Then add the following line to the "Control Source" of the text box where
you want the Age to appear:

=IIf(IsNull([DOB]),"",dhAge([DOB]))

The "DOB" is the field that has the birthday.
 
F

fredg

Is there a way to get the age of a client to appear next to the
birthdate?

I have a form, that people will fill in - once they filled in the
birthdate, is it possible to have a box next to it that will display
their age?

Im assuming that i'll need a code or something, where does that
usually go?

thanks for any help.


As the control source of an unbound control:
=DateDiff("yyyy",[DOB],Date())-IIf(Format([DOB],"mmdd")>Format(Date(),
"mmdd"),1,0)

Where [DOB] is the name of the field that stores the Date of Birth.

You do know, I hope, that this Age computation should NOT be stored in
any table.
Just compute it and display it on a form or report, as needed.
 
S

sea_minerals

Thank you.

It kinda of worked! On the form, once the birthdate is filled in the
age automatically appears - which is great!

When i view a report or the table, in the 'age' column it just has a
'0' is this right, or can it also display the age?

thanks again



Is there a way to get the age of a client to appear next to the
birthdate?
I have a form, that people will fill in - once they filled in the
birthdate, is it possible to have a box next to it that will display
their age?
Im assuming that i'll need a code or something, where does that
usually go?
thanks for any help.

As the control source of an unbound control:
=DateDiff("yyyy",[DOB],Date())-IIf(Format([DOB],"mmdd")>Format(Date(),
"mmdd"),1,0)

Where [DOB] is the name of the field that stores the Date of Birth.

You do know, I hope, that this Age computation should NOT be stored in
any table.
Just compute it and display it on a form or report, as needed.
 
G

Guest

Laymans Explanation:
As fredg and John have already said, we shouldn't be storing data that
changes in the database. For example - do you want to have to go through your
database every day to update individual ages when you can just calculate them
as needed ?

I've posted a detailed solution that I'm sure you will be able to use for
your problem 2 below...

Mæl.

Thank you.

It kinda of worked! On the form, once the birthdate is filled in the
age automatically appears - which is great!

When i view a report or the table, in the 'age' column it just has a
'0' is this right, or can it also display the age?

thanks again



Is there a way to get the age of a client to appear next to the
birthdate?
I have a form, that people will fill in - once they filled in the
birthdate, is it possible to have a box next to it that will display
their age?
Im assuming that i'll need a code or something, where does that
usually go?
thanks for any help.

As the control source of an unbound control:
=DateDiff("yyyy",[DOB],Date())-IIf(Format([DOB],"mmdd")>Format(Date(),
"mmdd"),1,0)

Where [DOB] is the name of the field that stores the Date of Birth.

You do know, I hope, that this Age computation should NOT be stored in
any table.
Just compute it and display it on a form or report, as needed.
 
F

fredg

Thank you.

It kinda of worked! On the form, once the birthdate is filled in the
age automatically appears - which is great!

When i view a report or the table, in the 'age' column it just has a
'0' is this right, or can it also display the age?

thanks again

Is there a way to get the age of a client to appear next to the
birthdate?
I have a form, that people will fill in - once they filled in the
birthdate, is it possible to have a box next to it that will display
their age?
Im assuming that i'll need a code or something, where does that
usually go?
thanks for any help.

As the control source of an unbound control:
=DateDiff("yyyy",[DOB],Date())-IIf(Format([DOB],"mmdd")>Format(Date(),
"mmdd"),1,0)

Where [DOB] is the name of the field that stores the Date of Birth.

You do know, I hope, that this Age computation should NOT be stored in
any table.
Just compute it and display it on a form or report, as needed.

On the report, simply repeat the calculation that you used on the
form.

There should NOT be an Age field in your table.
A stored Age is sure to be incorrect within, at most, 365 days.
 

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