Age of Employee Calculation

D

Douglas J. Steele

Add a calculated field to your query, and display that field:

Age: DateDiff("yyyy", [DOB], Date()) - IIf(Format(Date(), "mmdd") <
Format([DOB], "mmdd"), 1, 0)
 
M

Mark

Using employee birthdate information, how does one
program the form to update and display the actual age of
the employees in all records when the form is opened?

Thank you.
 
L

losmac

I'm doing it in this way:

rivate Sub Form_Current()
If Not IsNull(Me.BDay) Then Me.Age.Value = DateDiff
("yyyy", Me.BDay, Date)
End Sub
 
P

PC Datasheet

Base your form on a query and put the following expression in an empty field in
your query grid:

Age: DateDiff("yyyy", [DOB], Date()) - IIF(Format([DOB], "mmdd") >Format(Date(),
"mmdd"), 1, 0)


**Watch word wrap!
 
P

PC Datasheet

Doug,

Look at my response. Are you a follower of John Vinson too?

Steve
PC Datasheet


Douglas J. Steele said:
Add a calculated field to your query, and display that field:

Age: DateDiff("yyyy", [DOB], Date()) - IIf(Format(Date(), "mmdd") <
Format([DOB], "mmdd"), 1, 0)


--
Doug Steele, Microsoft Access MVP

(No private e-mails, please)



Mark said:
Using employee birthdate information, how does one
program the form to update and display the actual age of
the employees in all records when the form is opened?

Thank you.
 
D

Douglas J. Steele

I'm a respecter of John, if that counts. <g>

Actually, if I recall correctly, I was the first one to use
- IIf(Format(Date(), "mmdd") < Format([DOB], "mmdd"), 1, 0)

Most of the others were using

+ Int(Format(Date(), "mmdd") < Format([DOB]))

which relies on the fact that True is -1 and False is 0 in VBA. I started to
use the IIf construct because I didn't like relying on that fact. In VB.Net,
for instance, True is 1, not -1.

--
Doug Steele, Microsoft Access MVP

(No private e-mails, please)



PC Datasheet said:
Doug,

Look at my response. Are you a follower of John Vinson too?

Steve
PC Datasheet


Douglas J. Steele said:
Add a calculated field to your query, and display that field:

Age: DateDiff("yyyy", [DOB], Date()) - IIf(Format(Date(), "mmdd") <
Format([DOB], "mmdd"), 1, 0)


--
Doug Steele, Microsoft Access MVP

(No private e-mails, please)



Mark said:
Using employee birthdate information, how does one
program the form to update and display the actual age of
the employees in all records when the form is opened?

Thank you.
 
J

John W. Vinson

Douglas J. Steele said:
I'm a respecter of John, if that counts. <g>

And it's mutual, if THAT counts!
Actually, if I recall correctly, I was the first one to use
- IIf(Format(Date(), "mmdd") < Format([DOB], "mmdd"), 1, 0)

I believe that you are indeed the one from whom I learned this safer
technique.

(Just popping in from Google before going off to my nephew's
wedding... now I've got to go make him even more nervous!)

John W. Vinson
 
M

Mark

That is very helpful, and it handles the odd case where a
birthdate is not given.

Is there any way to program this code to update all the
records automatically when the form is opened?

Thanks
 
D

Douglas J. Steele

You don't want to store age in a table! Create a query based on that table,
and add a calculated field that computes their age everytime you run the
query. Use the query wherever you would otherwise have used the table.

By the way, the formula given in this post will be incorrect if the employee
hasn't had his/her birthday yet, due to how DateDiff works. And, of course,
it won't work in a query at all. You need to use something like:

Function CalculateAge(BirthDate As Variant) As Variant
If IsNull(BirthDate) Then
CalculateAge = Null
Else
CalculateAge = DateDiff("yyyy", Me.BDay, Date) - _
IIf(Format(Me.BDay, "mmdd") < Format(Date, "mmdd"), 1, 0)
End If
End If
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top