Calculate age from b-day to future date?

G

Guest

I am trying to figure out how to calculate the age in years between the
persons DOB and 4/1/2006. I know I need to use the DateDiff() thing, but I
am lost. I got the persons DOB put into a label on a form. But I need to
figure the age of the person based on their DOB and 4/1/2006. Then take the
age and put it onto another form. I just need to know how to get that age in
years using the datediff() thing? Thanks
 
R

Rick B

To calculate age as of today, you'd use...


DateDiff("yyyy",[Birthdate],Date())+(Format([Birthdate],"mmdd")>Format(Date(
),"mmdd"))






I'd just replace the "DATE()" with your date.
 
C

Chaim

Very nice way of determining whether you need to add 1 or not. Much neater
than doing the 'Iif (Month() > Month(), etc.' thing.

Definitely to be stashed in the bag of tricks.

--

Chaim


Rick B said:
To calculate age as of today, you'd use...


DateDiff("yyyy",[Birthdate],Date())+(Format([Birthdate],"mmdd")>Format(Date(
),"mmdd"))






I'd just replace the "DATE()" with your date.

--
Rick B



escudolm said:
I am trying to figure out how to calculate the age in years between the
persons DOB and 4/1/2006. I know I need to use the DateDiff() thing,
but
I
am lost. I got the persons DOB put into a label on a form. But I need to
figure the age of the person based on their DOB and 4/1/2006. Then take the
age and put it onto another form. I just need to know how to get that
age
in
years using the datediff() thing? Thanks
 
G

Guest

It tells me object does not support this property or method. The code looks
like this:

age = DateDiff("yyyy", [lbldob], 4 / 1 / 2006) + (Format([lbldob], "mmdd") >
Format(4 / 1 / 2006, "mmdd"))

lbldob is where I am pulling the b-day from a label. What did I do wrong?
 
R

Rick B

Where are you putting this? Is [lbldob] a field?

I don't think you need the "age=" if you are putting this formula in an
unbound field.
 
G

Guest

Rick B said:
Where are you putting this? Is [lbldob] a field?

I don't think you need the "age=" if you are putting this formula in an
unbound field.

Well, lbldob is where I am getting the DOB. Its a label that holds the DOB.
I thought that was where I had to put in the code to tell it where to pull
the DOB info. Should I not do it this way?
 
R

Rick B

I still don't understand. A label that holds the DOB? A value (like DOB)
is stored in a field in a table. You would pull that field and use it in
your calculation. That field where DOB is stored needs to be part of the
form's record source.

You still did not tell me where you are displaying the calculation though.
If you have a separate unbound field in your form, then you would simply put
"=" followed by your formula in that unbound field to display the age.

--
Rick B



escudolm said:
Rick B said:
Where are you putting this? Is [lbldob] a field?

I don't think you need the "age=" if you are putting this formula in an
unbound field.

Well, lbldob is where I am getting the DOB. Its a label that holds the DOB.
I thought that was where I had to put in the code to tell it where to pull
the DOB info. Should I not do it this way?
 
G

Guest

Well, I am scanning a barcode in a form, then have code to seperate and get
all the info. It puts the certain info into labels. The DOB info goes into
the label (lbldob). I was going to do the calculation after I found the DOB,
then once I get the age of the person, put it into a textbox onto another
form. This is what I have now:

lblage.Caption = DateDiff("yyyy", [lbldob], 4 / 1 / 2006) +
(Format([lbldob], "mmdd") > Format(4 / 1 / 2006, "mmdd"))

It doesn't work though. The code or calculation is being done on
frmlicense, then being transferred over to frmEntry in a textbox. It is
being done after all the info is found and seperated. Does that make sense
to you? By the way, thanks for the help so far.
 
R

Rick B

I don't understand why you are putting data in a label. (or how for that
matter)
Why are you not putting it into an unbound text field (or a bound text
field).
I don't even know if you can work with the label of a field.

So you mean you scan and put someone's birthdate into a label? What is the
field (associated with that label) used for? So you do this...


12/30/1968: ___________

I'm afraid we might be speaking two different languages here.

Put your birthdate into a field (if you are going to store it, put it in a
bound field on your form). Then, perform the calculation in an unbound text
field. And that's it. That's all there is to it. The user would see the
birthdate, and they would also see the age as of 4/1/2006. It would look
like this...


DOB: 12/30/1968
Age: 37

The DOB field is stored with your record. It is there to be used forever.

The AGE is simply a display for your user. You don't store it because it
would be redundant. You already have the DOB, so storing the age of the
person on a particular date can be calculated any time you need it. No need
to store both.
 
G

Guest

So, if I change the label into a textbox where the DOB is put into, I will be
better off? Then make another textbox and do the calculation for the age in
that textbox? Sorry for being confusing.
 
D

Douglas J. Steele

Dates need to be delimited with # characters:

age = DateDiff("yyyy", [lbldob], #4/1/2006#) + (Format([lbldob], "mmdd") >
Format(#4/1/2006#, "mmdd"))


--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)



escudolm said:
It tells me object does not support this property or method. The code
looks
like this:

age = DateDiff("yyyy", [lbldob], 4 / 1 / 2006) + (Format([lbldob], "mmdd")Format(4 / 1 / 2006, "mmdd"))

lbldob is where I am pulling the b-day from a label. What did I do wrong?
 

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