Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 360
  • Last Modified:

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!
0
howruaz9
Asked:
howruaz9
  • 2
2 Solutions
 
Dale FyeCommented:
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
 
Gustav BrockCIOCommented:
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
 
howruaz9Author Commented:
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
 
Gustav BrockCIOCommented:
You are welcome!

/gustav
0

Featured Post

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now