Option Compare Database
Option Explicit
Public Function Age(Arg_BirthDay, Optional Arg_BaseDay) As Variant
Dim tmpBirthDay As Date
Dim tmpBaseDay As Date
Age = Null
If IsMissing(Arg_BaseDay) Then
Arg_BaseDay = Date
End If
If IsNull(Arg_BirthDay) Or IsNull(Arg_BaseDay) Then
Exit Function
End If
If Not (IsDate(Arg_BirthDay) And IsDate(Arg_BaseDay)) Then
Exit Function
End If
If VarType(Arg_BirthDay) <> vbDate Then
tmpBirthDay = DateValue(Arg_BirthDay)
Else
tmpBirthDay = Arg_BirthDay
End If
If VarType(Arg_BaseDay) <> vbDate Then
tmpBaseDay = DateValue(Arg_BaseDay)
Else
tmpBaseDay = Arg_BaseDay
End If
If tmpBirthDay > tmpBaseDay Then
Exit Function
End If
If Format(tmpBaseDay, “mmdd”) < Format(tmpBirthDay, “mmdd”) Then
Age = Year(tmpBaseDay) - Year(tmpBirthDay) - 1
Else
Age = Year(tmpBaseDay) - Year(tmpBirthDay)
End If
End Function