Solved

Age Calculation

Posted on 2012-03-21
4
351 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
  • 2
4 Comments
 
LVL 47

Accepted Solution

by:
Dale Fye (Access MVP) earned 250 total points
Comment Utility
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 49

Assisted Solution

by:Gustav Brock
Gustav Brock earned 250 total points
Comment Utility
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
Comment Utility
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 49

Expert Comment

by:Gustav Brock
Comment Utility
You are welcome!

/gustav
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now