Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Calculate age in years and months from birthdate?

37 views
Skip to first unread message

Bucksy

unread,
Jan 4, 2005, 6:23:03 PM1/4/05
to
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.

Bucksy

unread,
Jan 4, 2005, 6:25:06 PM1/4/05
to

Douglas J. Steele

unread,
Jan 4, 2005, 7:03:12 PM1/4/05
to
Take a look at http://members.rogers.com/douglas.j.steele/Diff2Dates.html

--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no e-mails, please!)

"Bucksy" <Buc...@discussions.microsoft.com> wrote in message
news:BDFB9AAA-A8DC-4845...@microsoft.com...

Suzy

unread,
Aug 29, 2005, 12:40:02 AM8/29/05
to
Doug that page seems to have gone... any other suggestions / pages to look at?

Jeff Conrad

unread,
Aug 29, 2005, 12:50:16 AM8/29/05
to
Doug has now moved into a condo in Florida.
You can find him here:

http://www.accessmvp.com/djsteele/Diff2Dates.html

--
Jeff Conrad
Access Junkie - MVP
http://home.bendbroadband.com/conradsystems/accessjunkie.html
http://www.access.qbuilt.com/html/articles.html


"Suzy" wrote in message:
news:48374B7F-4FB1-41B9...@microsoft.com...

Suzy

unread,
Aug 29, 2005, 1:08:01 AM8/29/05
to
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!

Jeff Conrad

unread,
Aug 29, 2005, 1:35:14 AM8/29/05
to
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.

"Suzy" wrote in message:
news:73ECA8A3-4DE0-48EC...@microsoft.com...

Douglas J Steele

unread,
Aug 29, 2005, 7:21:00 AM8/29/05
to
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>

--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no e-mails, please!)


"Jeff Conrad" <je...@ernstbrothers.com> wrote in message
news:OmwgvtFr...@tk2msftngp13.phx.gbl...

Jeff Conrad

unread,
Aug 29, 2005, 11:57:23 AM8/29/05
to
"Douglas J Steele" wrote in message:
news:e%23nD$uIrFH...@TK2MSFTNGP15.phx.gbl...

> 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.

;-)

Suzy

unread,
Aug 29, 2005, 5:04:02 PM8/29/05
to
Hi Jeff and Doug, this works perfectly! Thank you SO much for your help.

Jeff Conrad

unread,
Aug 29, 2005, 5:18:10 PM8/29/05
to
"Suzy" wrote in message:
news:2BE3EAEC-6CE8-4B29...@microsoft.com...

> Hi Jeff and Doug, this works perfectly! Thank you SO much for your help.

You're welcome, glad we could help.

Smohrman

unread,
Feb 2, 2006, 2:29:41 PM2/2/06
to
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

fredg

unread,
Feb 2, 2006, 2:46:04 PM2/2/06
to

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.
--
Fred
Please respond only to this newsgroup.
I do not reply to personal e-mail

Smohrman

unread,
Feb 2, 2006, 3:56:28 PM2/2/06
to
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.

James A. Fortune

unread,
Feb 2, 2006, 4:22:28 PM2/2/06
to

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
MPAP...@FortuneJames.com

fredg

unread,
Feb 2, 2006, 5:10:57 PM2/2/06
to
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.....


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!

Smohrman

unread,
Feb 2, 2006, 5:52:23 PM2/2/06
to
Thanks fredg and James...I'm happy with how we have it working now.
Appreciate your time :-D

Lynn

unread,
Feb 12, 2006, 12:16:27 AM2/12/06
to
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

Douglas J. Steele

unread,
Feb 12, 2006, 5:46:24 AM2/12/06
to
You'll need to post the code you're trying to use.

--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele

(no private e-mails, please)


"Lynn" <Ly...@discussions.microsoft.com> wrote in message
news:A404F587-830F-4419...@microsoft.com...

Lynn

unread,
Feb 12, 2006, 5:55:33 PM2/12/06
to
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

unread,
Feb 12, 2006, 7:10:14 PM2/12/06
to
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
http://I.Am/DougSteele
(no private e-mails, please)


"Lynn" <Ly...@discussions.microsoft.com> wrote in message

news:97C59C22-4117-492C...@microsoft.com...

Lynn

unread,
Feb 12, 2006, 10:38:27 PM2/12/06
to
I named the module something entirely different.

No, I am running the code within VB. I guess that was my main question. I
have it in a "Select" event. I use the SQL statement that the query
generates and I put it in VB after: myRecordset.Open

My Access book says, "You don't have to base a recordset on a single table.
You can base it on a query. But you can't use the query's name in the
myRecordset.Open statement because only table names are allowed." It then
says to get the SQL statement from the query and paste it into VB after the
myRecordset.Open and inbetween the double quotes.

So that's what I did. But then I get that error. I know I am totally
confused here, so I hope I'm not confusing you. One more thing: I am not
putting a user-defined function into the Access query. The query works
perfectly. It generates the person's age by year, month and day. I just
need to translate that into VB so that it will populate the text box.

Thanks again!

Douglas J Steele

unread,
Feb 13, 2006, 7:42:46 AM2/13/06
to
Actually, you have succeeded in completely baffling me! <g>

I'm not even sure what you mean by "running the code within VB". I'm
assuming you mean VB6 (some people refer to code in Access as VB, even
though it's actually VBA)

Are you using a Jet database (an MDB) to hold the data, or are you going
against some other DBMS? If you're strictly going against Jet, DAO may make
more sense than ADO, and you can use a query name in the DAO OpenRecordset
method. (DAO is actually still the preferred method for dealing with Jet
databases)

The biggest error I see with your code is that you've named the recordset
myRecordset, yet you're referring to rs![Age]. That should be
myRecordset![Age]

Of course, there are other things that I don't understand about your code:
you instantiate myConnection, but you never open it, so that there's no way
for ADO to know where it's supposed to look for its data!

--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele

(no e-mails, please!)


"Lynn" <Ly...@discussions.microsoft.com> wrote in message

news:60079CAB-8D8E-44B4...@microsoft.com...

Brendan Reynolds

unread,
Feb 13, 2006, 8:40:30 AM2/13/06
to

In addition to the points that Doug raises, if the book says that, then
perhaps it is the book that is the source of confusion, as that statement
has no basis in reality - you most certainly can use the name of a saved
query with the Open method of an ADO recordset. Perhaps you've just been
reading the wrong book.

A further potential source of confusion is that in your most recent post you
say that you are not using a user-defined function in the Access query, but
the SQL you posted in the previous post includes a call to Doug's
user-defined function 'Diff2Dates'. As Doug says, that will only work within
Access, and I don't think your code is running within Access, as you mention
a 'Select event', and no built-in Access, ADO, or VBA object has a Select
event.

--
Brendan Reynolds
Access MVP

"Lynn" <Ly...@discussions.microsoft.com> wrote in message

news:60079CAB-8D8E-44B4...@microsoft.com...

Lynn

unread,
Feb 13, 2006, 4:17:27 PM2/13/06
to
Doug & Brendan:

I am so sorry, guys! I know so little about Access that I shouldn't even be
posting a question until I know a bit more. I really appreciate your help
and the fact that you didn't give up. This is way over my head, so I will
just drop it for now.

To answer your question, though: I am working in Visual Basic 6. I was
just trying to get the "Age" field from my Access query to populate a text
box in VB6. All my other Access table fields populate my VB6 text boxes just
fine. I think it's because I am working with a query and not a table that
it's not working. Again, I apologize.

Lynn

Brendan Reynolds

unread,
Feb 13, 2006, 4:25:46 PM2/13/06
to
No apology necessary, Lynn. I'd guess every developer has experienced the
difficulty of struggling with an unfamiliar application or technology at
times - I know I have.

--
Brendan Reynolds
Access MVP


"Lynn" <Ly...@discussions.microsoft.com> wrote in message

news:D6E16EFB-D7BF-4798...@microsoft.com...

0 new messages