473,385 Members | 1,610 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Automatic age calculation

Hello guys,
Thanks for all the help with the last question. I have another one. Does anyone know how to let access automatically calculate age and display in field when the person's date of birth is entered? Is that even possible? Any help would be greatly appreciated. Thanks.
Feb 25 '07 #1
13 14850
willakawill
1,646 1GB
Hi. this is what microsoft has to say on this subject
Feb 25 '07 #2
Hi. this is what microsoft has to say on this subject
Thank you Willa, I checked the site but what I actually want to do is to calculate the persons's age based on the computer system's date. So I will enter the person's birthday and access should use the system's date to calculate and display the person's age in years. Am not sure if this is possible but am still reading through some materials. Thanks again.
Feb 25 '07 #3
willakawill
1,646 1GB
You just need to put datediff on your form to calculate the age. The function, Now(), returns the system date and time.
Expand|Select|Wrap|Line Numbers
  1. DateDiff("y", #1-Feb-1995#, Now())
Feb 25 '07 #4
NDayave
92
I use this slightly longer code, but it returns the age as "15 Years 4 Months" for example.

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Public Function fnCalculateAge(strDateOfBirth As String)
  5.     Dim intYears As Integer
  6.     Dim intMonths As Integer
  7.     intYears = Year(Now) - Year(strDateOfBirth)
  8.     If Month(Now) < Month(strDateOfBirth) Or (Month(strDateOfBirth) = Month(Now) And Day(Now) < Day(strDateOfBirth)) Then
  9.         intYears = intYears - 1
  10.     End If
  11.     intMonths = Month(Now) - Month(strDateOfBirth)
  12.     If Day(Now) < Day(strDateOfBirth) Then
  13.         intMonths = intMonths - 1
  14.     End If
  15.     If intMonths < 0 Then
  16.         intMonths = intMonths + 12
  17.     End If
  18.     fnCalculateAge = intYears & " Years " & intMonths & " Months"
  19. End Function
Use as a module, and enter "=fnCalculateAge([Name of Text box with Date of Birth in])" in the text box on the form you want to display the age.

Obviously you need the Date of Birth text box on the form, but it can be invisible.

This may not be what you were looking for, but i thought id give you another option on the format of the Age calculated.

NDayave
Feb 25 '07 #5
NeoPa
32,556 Expert Mod 16PB
Set the Control Source of the TextBox you want it displayed in to :
Expand|Select|Wrap|Line Numbers
  1. =Year(Date)-Year(Me!DateOfBirth)-IIf(Format(Date,"mmdd")<Format(Me!DateOfBirth,"mmdd"),1,0)
DateDiff("yyyy",,) returns the nearest whole number of years.
Feb 26 '07 #6
NeoPa
32,556 Expert Mod 16PB
Different versions can be supplied to handle months in the age as well if required.
Feb 26 '07 #7
NDayave
92
Different versions can be supplied to handle months in the age as well if required.
How would you display it as Years and Months using the DateDiff?

Im guessing it would be shorter than my code, which is always a bonus

NDayave
Feb 27 '07 #8
Rabbit
12,516 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. (DateDiff("m", DOB, Date) \ 12) & " Years " & _
  2.    (DateDiff("m", DOB, Date) Mod 12) & " Months"
Feb 27 '07 #9
NeoPa
32,556 Expert Mod 16PB
That will work for the nearest value, which is probably more acceptable when measuring in months than in simple years, rather than whole months.
DateDiff() will always round to the nearest, rather than work in whole integer values.
Feb 27 '07 #10
thank you so much guys. i really appreciate the help.
Mar 4 '07 #11
willakawill
1,646 1GB
thank you so much guys. i really appreciate the help.
You are very welcome :)
Mar 4 '07 #12
New to VBA code and this worked PERFECTLY!
Thank you!!

__________________________________________________ _________

Re: Automatic age calculation
--------------------------------------------------------------------------------

I use this slightly longer code, but it returns the age as "15 Years 4 Months" for example.
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Public Function fnCalculateAge(strDateOfBirth As String)
  5.     Dim intYears As Integer
  6.     Dim intMonths As Integer
  7.     intYears = Year(Now) - Year(strDateOfBirth)
  8.     If Month(Now) < Month(strDateOfBirth) Or (Month(strDateOfBirth) = Month(Now) And Day(Now) < Day(strDateOfBirth)) Then
  9.         intYears = intYears - 1
  10.     End If
  11.     intMonths = Month(Now) - Month(strDateOfBirth)
  12.     If Day(Now) < Day(strDateOfBirth) Then
  13.         intMonths = intMonths - 1
  14.     End If
  15.     If intMonths < 0 Then
  16.         intMonths = intMonths + 12
  17.     End If
  18.     fnCalculateAge = intYears & " Years " & intMonths & " Months"
  19. End Function
Use as a module, and enter "=fnCalculateAge([Name of Text box with Date of Birth in])" in the text box on the form you want to display the age.

Obviously you need the Date of Birth text box on the form, but it can be invisible.

This may not be what you were looking for, but i thought id give you another option on the format of the Age calculated.

NDayave
Jan 27 '08 #13
NeoPa
32,556 Expert Mod 16PB
I assume from the signature that this is actually NDayave. Let me know if you're having trouble with your account.

(also try to remember to use the [ CODE ] tags in your posts please).
Jan 27 '08 #14

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Aspersion | last post by:
I'm building an ASP page that has a lot of text and graphics. There is a calculation facility on the page. The user enters several numbers in a form and presses a button to see the calculated...
0
by: anaxamandr | last post by:
Hi. I have a long loop in ASP that performs a rather lengthy calculation. I would love for my users to be able to stop that calculation, if they so choose, mid way through the process. I attempted...
2
by: Del | last post by:
Thanks in advance for any help. I have a database that was created in Access 2000. Several users have been upgraded to Access 2003. Since upgrading to 2003 we have noticed that some of the...
4
by: Michiel Alsters | last post by:
Hello everybody, I hope anybody can help me. I'll try to give a brief overview of my problem. I have running a program that performs a heavy calculation. To give the user feedback what the...
4
by: vg-mail | last post by:
Hello all, I have identical design for form and report but I am getting calculation error on form and everything is OK on report. The form and report are build up on SQL statement. The...
5
by: The alMIGHTY N | last post by:
Hi all, Let's say I have a simple math formula: sum (x * y / 1000) / (sum z / 1000) I have to do this across 50 items, each with an x, y and z value, when the page first loads AND when a...
3
by: mattmao | last post by:
Okay, I was asked by a friend about the result of this limit: http://bbs.newwise.com/attdata/forumid_14/20070922_fe7f77c81050413a20fbDWYOGm7zeRj3.jpg Not n->zero but n-> + infinite I really...
17
by: Sunburned Surveyor | last post by:
I was thinking of a way I could make writing Python Class Files a little less painful. I was considering a Ptyhon script that read a file with a list of property names and method names and then...
3
by: myjish18 | last post by:
Hello, We have a DB2 UDB database v8.2.7 (db2 v8.2 fixpak 14) on AIX 5.3 which has Automatic Storage (AS) enabled. We want to disable automatic storage on entire database and/or disable...
0
by: vamsioracle | last post by:
Hi all I have a disco Report with two sheets. In sheet 1, i used a function from database that inserts data (based on some calculation) into a table. In sheet 2, the column inserted by function in...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
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,...
0
Oralloy
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,...
0
jinu1996
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 using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.