Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Age Calculation

Posted on 2012-03-21
4
Medium Priority
?
359 Views
Last Modified: 2012-03-21
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!
0
Comment
Question by:howruaz9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 48

Accepted Solution

by:
Dale Fye earned 1000 total points
ID: 37748341
That error message implies that the the [DOB] passed to the function is NULL.

I would recommend that you type your variables and modify your function to accomdate NULL values, something like:

Function Age(Bdate as Variant, Optional DateToday as Variant = NULL) As Variant

' Returns the Age in years between 2 dates
' Doesn't handle negative date ranges i.e. Bdate > DateToday

    If IsNull(BDate) then
        Age = NULL
        Exit Function
    End if

    if isnull(DateToday) then DateToday = Date()

    'pass a -1 back to the calling function if the bDate is greater than
    'the 2nd date parameter
    if bDate > DateToday then
        Age = -1
    Else
        Age = DateDiff("yyyy", bDate, DateToday) + (Format(bDate, "mmdd") > Format(DateToday, "mmdd")
    End if
End Function

The way this calculation works is to compute the date difference in years between two dates.  But the Datediff() function actually just subtracts one year from the next so it will return 1 if passed values of #12/31/11# and #1/1/12#.  So, you then have to determine whether the birthday (month/day) has actually passed in the current year.  The syntax:

(Format(bDate, "mmdd") > Format(DateToday, "mmdd")

Will return a -1 if the birthday has not occurred yet in the current year.
0
 
LVL 51

Assisted Solution

by:Gustav Brock
Gustav Brock earned 1000 total points
ID: 37748384
You could use the function below like this:

Age: IIf([YourDOBfield] Is Null, Null, AgeSimple([YourDOBfield]))

Public Function AgeSimple( _
  ByVal datDateOfBirth As Date) _
  As Integer

' Returns the difference in full years from datDateOfBirth to current date.
'
' Calculates correctly for:
'   leap years
'   dates of 29. February
'   date/time values with embedded time values
'
' DateAdd() is used for check for month end of February as it correctly
' returns Feb. 28. when adding a count of years to dates of Feb. 29.
' when the resulting year is a common year.
' After an idea of Markus G. Fischer.
'
' 2007-06-26. Cactus Data ApS, CPH.

  Dim datToday  As Date
  Dim intAge    As Integer
  Dim intYears  As Integer
    
  datToday = Date
  ' Find difference in calendar years.
  intYears = DateDiff("yyyy", datDateOfBirth, datToday)
  If intYears > 0 Then
    ' Decrease by 1 if current date is earlier than birthday of current year
    ' using DateDiff to ignore a time portion of datDateOfBirth.
    intAge = intYears - Abs(DateDiff("d", datToday, DateAdd("yyyy", intYears, datDateOfBirth)) > 0)
  End If
  
  AgeSimple = intAge
  
End Function

Open in new window

/gustav
0
 

Author Closing Comment

by:howruaz9
ID: 37748536
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!
0
 
LVL 51

Expert Comment

by:Gustav Brock
ID: 37748549
You are welcome!

/gustav
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question