I know it's been asked and answered, but I'm still not following. I'm an accountant using MS Access to create an Human Resources database. I am looking for the DATEDIFF formula to calculate the age of a person in Years, Months, and Days. I've copied and pasted formulas I've found on the net and am NOT getting the right results, though the format (Y-M-D) is correct. For example, I am 46 years, 5 months, and 1 day old, yet the answer I get is 47 years, 7 months, and 199 days.
Here is the formula:
=DateDiff("yyyy",[Birth Date],Date()) & " yr(s). " & DateDiff("m",[Birth Date],Now())-(DateDiff("yyyy",[Birth Date],Now())*12) & " mo(s)." & DateDiff("d",[Birth Date],Now())-(DateDiff("yyyy",[Birth Date],Now())*365) & " day(s)"
Any help (like actually writing the formula so I can copy and paste) would be greatly appreciated. Thanks.
7 15895
The problem is parsing out the individual fields. You need to do one DateDiff for the whole, then get the parts. I imagine the 7 months and 199 days might include a negative sign on one or both (indicating it is 47 years, minus 7 months and/or 199 days. This would happen if you parse out months and get January(1) - August(8) = 7.
You are correct... 47 yrs. -7 mos. -199 days.
No result is going to be exact, since Access doesn't have a built-in feature for this, but the code below should get you within +/- 1 day. -
Dim dblYears As Double
-
Dim intYears As Integer
-
Dim dblMonths As Double
-
Dim intMonths As Integer
-
Dim intDays As Integer
-
-
intDays = DateDiff("d", Me.[Birth_Date], Date)
-
dblYears = intDays / 365.25
-
intYears = Fix(dblYears)
-
dblMonths = (dblYears - intYears) * 12
-
intMonths = Fix(dblMonths)
-
intDays = ((dblMonths - intMonths) / 12) * 365
-
-
Me.txtResult = intYears & " years, " & intMonths & " months, " & intDays & " days"
-
jforbes 1,107
Recognized Expert Top Contributor
You also might find these useful: Age() Function - Public Function getAge(ByVal vDateOfBirth As Variant, ByVal vDateReference As Variant) As String
-
-
Dim iDays As Integer
-
Dim iMonths As Integer
-
Dim iYears As Integer
-
Dim iDaysInMonth As Integer
-
Dim dDateOfBirth As Date
-
Dim dDateReference As Date
-
-
dDateOfBirth = Int(Nz(vDateOfBirth, Now()))
-
dDateReference = Int(Nz(vDateReference, Now()))
-
-
iDaysInMonth = DateSerial(Year(dDateOfBirth), Month(dDateOfBirth) + 1, 1) - DateSerial(Year(dDateOfBirth), Month(dDateOfBirth), 1)
-
-
iYears = DatePart("yyyy", dDateReference) - DatePart("yyyy", dDateOfBirth)
-
iMonths = DatePart("m", dDateReference) - DatePart("m", dDateOfBirth)
-
iDays = DatePart("d", dDateReference) - DatePart("d", dDateOfBirth)
-
If iDays < 0 Then
-
iMonths = iMonths - 1
-
iDays = iDaysInMonth + iDays
-
End If
-
If iMonths < 0 Then
-
iYears = iYears - 1
-
iMonths = 12 + iMonths
-
End If
-
-
getAge = iYears & " yr(s). " & iMonths & " mo(s). " & iDays & " day(s)"
-
End Function
I'm assuming the above is VBA code. If so, where do I copy and paste that? Not sure how this all works. Thanks.
You would add the jforbes code to your VBA script on the applicable form, then call it using the getAge([Birth_Date]) command.
My code could be added to VBA using an event (button on_click or a field after_update, for example.
It worked! Thanks so much for your help.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: carl.barrett |
last post by:
Hi,
I have a continuous form based on a query ( I will also be creating a
report based on the same query).
There are 2 fields:
Date Obtained and Date Of Expiry
I want a further 3 columns...
|
by: Juan |
last post by:
Hi everyone,
is there a function that calculates the exact amount of Years, Months, and
Days between two days?
TimeDiff is not good enough, since it calculates, for example:
Date 1: Dec....
|
by: Adrian |
last post by:
I hit on this problem converting a VB.NET insurance application to C#.
Age next birthday calculated from date of birth is often needed in
insurance premium calculations.
Originally done using...
|
by: Jose |
last post by:
Exists a function that determined between two dates the years,months and
days?
Thanks a lot.
|
by: Libber39 |
last post by:
Hi everyone,
Have a query on how to calculate the amount of weeks and days contained in a number in an access query. ie: the difference in days between 2 dates amounts to 17 days. I want to now...
| |
by: karthickkuchanur |
last post by:
Hai sir,
i assigned to calculate the age from current date ,i have the date of birth of employee from that i have to calculate the age dynamically,
i refer to google it was confusing...
|
by: johanneguaybenoit |
last post by:
Hi
I would like to know how to create a function or module to calculate
the age of a person in years months days.
and hours if feasable
But I really nead to know in years months days.
I...
|
by: Louise Harrison |
last post by:
I had read a previous article on calculating the number of years, months and days between 2 dates. They had used the DateDiff with a start date and an end date, similiar like below statement:
...
|
by: wilson52 |
last post by:
I'm trying to update a form field to calculate number of days overdue based on another projected training date field on my form.
This is what I have on my control source line:
...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| | |