Calcualting a date

  • Thread starter Thread starter asmart
  • Start date Start 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
 
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
 
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
 
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
 
Back
Top