Solved

Age Calculation

Posted on 2012-03-21
4
357 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 250 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 250 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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

627 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