Calculate age in years and months from birthdate?

B

Bucksy

I have a field for birthdate (ddmmyyyy) and I want to make a field (or form
control) that will calculate the age in years and months from the birthdate.
MS has a sample that does years.
 
S

Suzy

Thank you Jeff... could you tell me how on earth to use all that information.
I'm a beginner user, muddling my way through things I really don't
understand!

I have a table with staff database with a column for "birthdate" and I am
wanting to report the age in years and months within a report showing each
individual's personal details.

Me thinks I'm in over my head!
 
J

Jeff Conrad

Sure Suzy, here is what you need to do.

1. Make a backup of your database (always a good thing).

2. Copy/paste all of that code on Doug's page to a new standard
module. Just copy the part starting on the line that says:
'***************** Code Start **************

.....and down to the bottom.

3. Compile the code, save and close the module. Name the module
basDougDateFunction

4. Now in your report I assume you already have a field in the recordsource
called Birthdate. Add a new unbound textbox into the Detail section.
Position it where you want and call it txtAge.

5. Enter this into the Control Source for txtAge:
=Diff2Dates("ym",[Birthdate],Now())

What this will do is display the age in years and months based on the
entry in the Birthdate field.

6. Save and close the report and then test.

You should be good to go.
--
Jeff Conrad
Access Junkie - MVP
http://home.bendbroadband.com/conradsystems/accessjunkie.html
http://www.access.qbuilt.com/html/articles.html

in message:
 
D

Douglas J Steele

Thanks for covering for me, Jeff. However, I thought you knew that under the
Witness Protection Program, you weren't supposed to reveal my whereabouts!
<g>
 
J

Jeff Conrad

in message:
Thanks for covering for me, Jeff. However, I thought you knew that under the
Witness Protection Program, you weren't supposed to reveal my whereabouts!
<g>

Oops, sorry.
Better not unpack Doug.

;-)
 
S

Smohrman

Hi Jeff,

I am looking to implement the solution you helped Suzie with, and am having
a little trouble past a couple of steps. I have a database in which I need
to calculate the same thing, and I have created the module per your
instructions, but I don't know what you mean by "a new unbound textbox into
the Detail section. Position it where you want and call it txtAge. Enter
this into the Control Source for txtAge..."

I'm trying to include this function in a table, and don't understand the
steps to create the text box, and then where to enter the Control Source
info. I'm almost there! Can you point me in the right direction from where
I'm at? Anyone?

Thanks in advance...

Scott
 
F

fredg

Hi Jeff,

I am looking to implement the solution you helped Suzie with, and am having
a little trouble past a couple of steps. I have a database in which I need
to calculate the same thing, and I have created the module per your
instructions, but I don't know what you mean by "a new unbound textbox into
the Detail section. Position it where you want and call it txtAge. Enter
this into the Control Source for txtAge..."

I'm trying to include this function in a table, and don't understand the
steps to create the text box, and then where to enter the Control Source
info. I'm almost there! Can you point me in the right direction from where
I'm at? Anyone?

Thanks in advance...

Scott

You DO NOT do this in any table.
If you store the Age then it is sure to be incorrect at least once a
year.

Table are used to store data, not to perform calculations.
As long as you have the date of birth stored, you can compute the age
as instructed, in a form, query or report, as Jeff stated.

You do it in the report itself.

Add an UNBOUND text control to your report (Click on the ToolBox tool
button. Click on the Text control (marked Ab). Drag it onto your
report.
Look in the Control's properties Data tab.
Click on the Control Source line.
Write whatever Jeff suggested (as it's not included in your message
here, I can't be specific).
Something like ....
= Diff2Dates(etc.)

Run the report. If you have correctly entered the Diff2Dates
arguments, you will see the age in years and months for each person in
the report.
 
S

Smohrman

Thanks for that clarification. I am now working with a query, and have
gotten some code working that returns a value of months since a date:

Age in Months: DateDiff("m",[AquisitionDate],Now())

This gives us the age of the item in months. Ideally I’d like to use
something like this:

Age=DateDiff("yyyy", [AquisitionDate], Now())+ _
Int( Format(now(), "mmdd") < Format( [AquisitionDate], "mmdd") )

which should give the age in Years/Months, but when I try it I get an error
stating that I “Omitted an operand or operator, used an invalid character or
comma, or didn’t surround text with quotes...†I tried changing Age= to Age:
DateDiff like in the code above which works but that didn’t fix it.

Thanks for your help.
 
J

James A. Fortune

Smohrman said:
Hi Jeff,

I am looking to implement the solution you helped Suzie with, and am having
a little trouble past a couple of steps. I have a database in which I need
to calculate the same thing, and I have created the module per your
instructions, but I don't know what you mean by "a new unbound textbox into
the Detail section. Position it where you want and call it txtAge. Enter
this into the Control Source for txtAge..."

I'm trying to include this function in a table, and don't understand the
steps to create the text box, and then where to enter the Control Source
info. I'm almost there! Can you point me in the right direction from where
I'm at? Anyone?

Thanks in advance...

Scott

As an alternative, the logic from

http://groups.google.com/group/microsoft.public.access/msg/cf857b8e3aa9c6b0

can be used if you replace DateAdd('d', N, Date()) with the final date
and Date() with the BirthDate. Note that the 'AS' keyword in SQL is
used to give an expression a name in a way that is equivalent to using a
name with a colon after it in the QBE query design window.

James A. Fortune
(e-mail address removed)
 
F

fredg

On Thu, 2 Feb 2006 12:56:28 -0800, Smohrman wrote:

See comments interspersed below as well as at the end of this message.
Thanks for that clarification. I am now working with a query, and have
gotten some code working that returns a value of months since a date:

Age in Months: DateDiff("m",[AquisitionDate],Now())

This gives us the age of the item in months. Ideally Iÿd like to use
something like this:

Age=DateDiff("yyyy", [AquisitionDate], Now())+ _
Int( Format(now(), "mmdd") < Format( [AquisitionDate], "mmdd") )
which should give the age in Years/Months,

No it will not give you the age in years and months.
And that is not how you write and expression in a query.
but when I try it I get an error
stating that I ´Omitted an operand or operator, used an invalid character or
comma, or didnÿt surround text with quotes...¡ I tried changing Age= to Age:
DateDiff like in the code above which works but that didnÿt fix it.

If you are in the query grid you do not start with the = sign. Use:

Age:DateDiff(etc....)

More comments at the end.....
Thanks for your help.


1) To accurately compute Age by year, you must take into account
whether or not that date has already occurred in the current year.
DateDiff by itself does not take that into account, i.e.
DateDiff("yyyy",#12/31/2005#,#1/1/2006#) will = 1.

For someone born on 9/15/1980, the DateDiff function will return
either years, (26), or months, (305), or days ( 9271), depending upon
the argument you supply it. (Note that the actual age in years should
be 25, not 26, as the birth date has not yet occurred this year).

To accurately calculate someone's age in years only, the following
will be accurate.

In a query:
Age: DateDiff("yyyy", [DOB], Date()) - IIF(Format([DOB], "mmdd") >
Format(Date(), "mmdd"), 1, 0)

2) You are using Now() to represent the current date. Now() includes a
time of day value and you may get different results depending upon the
actual time of day you run the query. Always use Date() if the time of
day is not needed.

3) The post I responded to had a reference to Doug Steele's Diff2Dates
function at:
http://www.accessmvp.com/djsteele/Diff2Dates.html
not the DateDiff function.

The Diff2Date function will return the age in years and months and
days, all at once, depending upon the arguments you supply it.
Some one born on 9/15/1980 will be
"25 years 4 months 18 days" old today.
Isn't that what you want according to your message subject line?

If you want that then you must use the Diff2Dates function.

Download the function and copy it to a Module.
Refer to it in your query grid like this:
AgeToday:Diff2Dates("ymd",[DateOfBirth],Date(),True)
or like this (notice the different results):
AgeToday:Diff2Dates("ymd",[ADate],Date())

Good luck!
 
S

Smohrman

Thanks fredg and James...I'm happy with how we have it working now.
Appreciate your time :-D
 
L

Lynn

My question is similar but it has to do with connecting the database with
Visual Basic. I built a query to determine a person's age per the above
instructions. It works perfectly (thank you). I copied the SQL code and
put it in my VB code after "myRecordset.open." I want it to fill the textbox
(in VB) with the person's age. Now it gives me the error: "Item cannot be
found in the collection corresponding to the recordset." I have other
databases that work fine in VB and fill out textboxes just fine. I
triple-checked the spelling of the field names, etc., and they all match.
Can anyone help?

Thanks,

Lynn
 
L

Lynn

Doug:

Thank you so much for getting back to me. I spent all day yesterday reading
about Access queries. I program in VB6 and have very little experience in
Access. Here is my code:

Dim myConnection As ADODB.Connection
Set myConnection = New Connection
Dim myRecordset As New ADODB.Recordset
myRecordset.ActiveConnection = myConnection

' Open PatientInfo Query

myRecordset.Open "SELECT PatientInfo.PatientID, PatientInfo.Birthdate,
Diff2Dates('ymd',[Birthdate],Now()) AS Age FROM PatientInfo", dBase,
adOpenDynamic, adLockOptimistic

myRecordset.Filter = "(PatientID) like '" & strPatientID & "*'"
frmPatientInfo.txtAge.Text = rs![Age]

myRecordset.Close

I have the "Open" statement on one line. I set a break at rs![Age] and it
gives me that "Item cannot be found..." error. Can you please tell me what I
am doing wrong? I'm curious, how does VB know what query to look in? I see
where it refers to the table, but not the query. Any help would be greatly
appreciated!

Lynn
 
D

Douglas J. Steele

When you copied the code for Diff2Dates into your application, what did you
name the module? If you named the module Diff2Dates, rename it: modules
can't be named the same as functions or subs in them.

I'm assuming you're actually running this code from within Access, not from
a VB program. You can't put user-defined functions into Access queries if
you're running them from outside of Access.

--
Doug Steele, Microsoft Access MVP

(no private e-mails, please)


Lynn said:
Doug:

Thank you so much for getting back to me. I spent all day yesterday
reading
about Access queries. I program in VB6 and have very little experience in
Access. Here is my code:

Dim myConnection As ADODB.Connection
Set myConnection = New Connection
Dim myRecordset As New ADODB.Recordset
myRecordset.ActiveConnection = myConnection

' Open PatientInfo Query

myRecordset.Open "SELECT PatientInfo.PatientID, PatientInfo.Birthdate,
Diff2Dates('ymd',[Birthdate],Now()) AS Age FROM PatientInfo", dBase,
adOpenDynamic, adLockOptimistic

myRecordset.Filter = "(PatientID) like '" & strPatientID & "*'"
frmPatientInfo.txtAge.Text = rs![Age]

myRecordset.Close

I have the "Open" statement on one line. I set a break at rs![Age] and it
gives me that "Item cannot be found..." error. Can you please tell me
what I
am doing wrong? I'm curious, how does VB know what query to look in? I
see
where it refers to the table, but not the query. Any help would be
greatly
appreciated!

Lynn

Douglas J. Steele said:
You'll need to post the code you're trying to use.
 

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