Link to home
Start Free TrialLog in
Avatar of howruaz9
howruaz9Flag for Canada

asked on

Age Calculation

Hi experts,

I got the following from EE (pete's posting) and it worked fine until I tried to set >=18 on the criteria, the pop-up screen said: run-time error "94", Invalid use of Null, and "Age = Year(DateToday) - Year(Bdate)" was in yellow. Please help!


Function Age(Bdate, DateToday) As Integer
' Returns the Age in years between 2 dates
' Doesn't handle negative date ranges i.e. Bdate > DateToday

    If Month(DateToday) < Month(Bdate) Or (Month(DateToday) = _
                Month(Bdate) And Day(DateToday) < Day(Bdate)) Then
            Age = Year(DateToday) - Year(Bdate) - 1
    Else
            Age = Year(DateToday) - Year(Bdate)
    End If
End Function

Save and close the module.

To use this in a query add a new column:

Age at 31-08-2003: Age([DOB],"31-Aug-2003")

Thank you!
ASKER CERTIFIED SOLUTION
Avatar of Dale Fye
Dale Fye
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of howruaz9

ASKER

Hi fyed and gustav, thank you both for your suggestions, I tried both and made some neccessary
changes according to my situation. They are all good.

Thank you very much!
You are welcome!

/gustav