Calcualting a date

A

asmart

Hi, I need help calculating a field on a form......we
enter the birthdate of the member, I want it to calculate
the age of the member, sounds simple but I cant get it to
work!....

Help!

Asmart
 
L

losmac

Private Sub Test_Click()
Dim enteredDate As Date
Dim todayDate As Date
Dim memberAge As Long

If IsNull(Me.Text0) Then Exit Sub

enteredDate = Me.Text0
todayDate = Date

memberAge = DateDiff("yyyy", enteredDate, todayDate) 'diff
in years

Me.Text2 = memberAge
End Sub
 
O

OzPete

Hi Asmart

For the purpose of demonstration, I am assuming that your date of birth
field is called 'DOB'- [DOB] - thus:

create an unbound text field, delete the label or change it to say 'Age',
and the control source of the unbound text box is

=DateDiff("yyyy",[DOB],Now())

Create an 'After Update' event procedure to say

Private Sub DOB_AfterUpdate()
Me.Refresh
End Sub

Naturally, if your [DOB] field is called something different, then replace
[DOB] with your text field name. Sometimes it helps to post the name of
the fields, so that you can be supplied with more exact code. This code
works for me on one of my forms, so it should for you...

hope that helps

OzPete
 
O

OzPete

Sorry Asmart,

Forgot to say that this applies to the After Update event for the DOB
field.....

create an unbound text field, delete the label or change it to say 'Age',
and the control source of the unbound text box is

=DateDiff("yyyy",[DOB],Now())

Create an 'After Update' event procedure to say

Private Sub DOB_AfterUpdate()
Me.Refresh
End Sub

</snip>

OzPete
 

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