472,785 Members | 1,005 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,785 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 14720
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,534 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,534 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,534 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,534 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: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.