Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Auto update age

Status
Not open for further replies.

JMM

Technical User
Feb 17, 2000
37
NZ
I have 2 fields. DOB and Age. I have code which calculates age after DOB is entered.<br>

What I haven't figured out is how to connect DOB with Now() so that everytime the form is opened the age is recalculated as the month changes over.<br>

<br>

Help appreciated.
 
I hope you are not storing the age in a table. This is not necessary, you calculate the age on the fly. Here is one way of doing it, assuming you only want the age in years.<br>
<br>
Add the following in the control source of a text box:<br>
=Format(Now()-[DOB],&quot;yy&quot;)<br>
<br>
HTH<br>
RDH <p>Ricky Hicks<br><a href=mailto: rdhicks@mindspring.com> rdhicks@mindspring.com</a><br><a href= > </a><br>
 
Ricky, Thanks for your response.<br>
Age in the table is necessary. The code I have worked out calculates month as well.<br>
The issue is that the form may not be opened very frequently. The age as calculated at the time of entering the DOB would be correct but it wouldn't be weeks or months later. <br>
So, what I need help with is how, everytime that form is opened, to automatically update, not the DOB, but the age 'as of now'.<br>
<br>
Thanks again.
 
put that code in the &quot;On_current&quot; event of the form as well.<br>
So when either the form is opened or you move to the next record it will update. <p> DougP<br><a href=mailto: dposton@universal1.com> dposton@universal1.com</a><br><a href= > </a><br>
 
I have a similar situation--my form has a DOB field and DOD field. Age is a calculated field on the form also and is updated according to today's date.<br>
<br>
Below is the control source for the age text box. The .25 takes into account for leap years. It also stops the age field from updating if the person is deceased.<br>
<br>
=IIf(IsNull([date_death]),Fix(((Now()-[date_birth])/365.25)),Fix((([date_death]-[date_birth])/365.25))) <p>jgarnick<br><a href=mailto:jgarnick@aol.com>jgarnick@aol.com</a><br><a href= > </a><br>
 
Another thought--are you storing age in a table? Is this for reporting purposes? It's a bit risky since you could have incorrect info unless the form was opened up for each person that was to be on the report. Why not just use a calculated field on your report when in need of the age field and keep it out of the table?? Also, it only takes one day for you to be older, not a month! Good luck! <p>jgarnick<br><a href=mailto:jgarnick@aol.com>jgarnick@aol.com</a><br><a href= > </a><br>
 
For exactly the reason you mention, that you cannot ensure accuracy at the table level, in most cases it's a big NoNo to store calculated fields in the same table as the fields used to calculate them. If want to display the current age when the form is opened, just do it without using a bound field. If you're concerned about a reports or forms displaying it differently, build it as a global variable in a public function and call that from everywhere.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top