Display age in years, months and days

Bradon

Board Regular
Joined
Dec 18, 2002
Messages
66
I have found the code to convert date of birth to years on the forum:

Private Sub DateofBirth_BeforeUpdate(Cancel As Integer)
If Date >= DateSerial(Year(Date), Month(Me![DateofBirth]), Day(Me![DateofBirth])) Then
Me![age] = DateDiff("yyyy", Me![DateofBirth], Date)
Else
Me![age] = DateDiff("yyyy", Me![DateofBirth], Date) - 1
End If
End Sub

I would like to display the result to show years, months and days
i.e 10 years 7 months 17 days

Any suggestions as to how to do this?
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Thanks Peter - I followed the link but couldn't understand where the code should be put. How do you find or create a new module?

I was hoping to be able to extend the original code.

Sorry but I'm still learning access.
 
Upvote 0
The layout varies a bit depending on which version you have, but click on the Modules tab then click the new button.
paste the code in to the new module and save it. do not give the module the same name as a function or Access will sulk.
You can then call the function from your code the same as you would any other -- Age(Date1, Date2)

Code:
Private Sub DateofBirth_BeforeUpdate(Cancel As Integer)
     Me![Age] = Age(Me![DateofBirth], DATE)
End Sub

You could build all of the code into your existing code but keeping functions seperate allows you to call them from other forms if needed and it also makes it easier to copy to new DB'S when you have to develop them.


HTH
Peter
 
Upvote 0

Forum statistics

Threads
1,221,808
Messages
6,162,097
Members
451,742
Latest member
JuanMark10

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top