In a recent post of 11-Nov-2008, Michael Red provided the code for calculating age from "date of birth" to "date of death or current date".
After finally placing the code in the correct module with the correct code for calling the function, I think I've isolated the last annoying problem.
In correlation with my customized function code below:
1) The code will display #Error if neither date is supplied in the underlying table - the way it should, no problem
2) When using the call: =basDOB2AgeExt([DOB]), the code will display age from the date of birth (DOB) to the present time. Okay for some situations, but not my particular situation.
3) When using the call: =basDOB2AgeExt([DOB],[DOD]) there are two possible results:
a) If a date of death (DOD) *is* provided by the underlying table, age is nicely, precisely provided.
b) If no date of death (DOD) is provided by the underlying table, #Error is displayed in the control. Not so good.
I'm confused as to what the "If" code in the function is doing. To me, the code appears as if - if [DOD] = -1 (true), then it should = Date. But shouldn't it be if [DOD] = 0 (false), then it should = Date?
Any advice will be greatly appreciated.
Here's my customized code:
Public Function basDOB2AgeExt(DOB As Date, Optional DOD As Date = -1) As String
'Michael Red 5/23/02
'DOB is just the date of Birth
'DOD is an optional date to check - as in examples 1 & 2
'Otherwise, the DOB is checked against the Current Date
'? basDOB2AgeExt(#8/21/42#)
'59 Years 9 Months and 2 Days.
'? basDOB2AgeExt(#8/21/1942#, #8/20/2022#)
'79 Years 11 Months and 30 Days.
'? basDOB2AgeExt(#8/21/1942#, #8/21/2022#)
'80 Years 0 Months and 0 Days.
Dim tmpAge As Integer 'Simple DateDiff w/o Birthday correction
Dim tmpDt As Date 'Date to use in intermediate Calcs
Dim DtCorr As Boolean 'BirthDay Before or After date in question
Dim YrsAge As Integer
Dim MnthsAge As Integer 'Additional Mnths
Dim DaysAge As Integer 'Additional Days
If (DOD = -1) Then 'Check for (Optional Date to Check against)
DOD = Date 'If Not Supplied, Assume Today
End If
tmpAge = DateDiff("YYYY", DOB, DOD) 'Just the Years considering Jan 1, Mam
DtCorr = DateSerial(Year(DOD), Month(DOB), Day(DOB)) > DOD
YrsAge = tmpAge + DtCorr 'Just Years and Correction
tmpDt = DateAdd("yyyy", YrsAge, DOB)
MnthsAge = DateDiff("m", tmpDt, DOD)
DtCorr = DateAdd("m", MnthsAge, tmpDt) > DOD
MnthsAge = MnthsAge + DtCorr
tmpDt = DateAdd("m", MnthsAge, tmpDt)
DaysAge = DateDiff("d", tmpDt, DOD)
basDOB2AgeExt = YrsAge & " Years " & MnthsAge & " Months and " & DaysAge & " Days."
End Function
randy700 (Programmer)
Called by:
=basDOB2AgeExt([DOB]) 'no DOD value passed, use current date.
=basDOB2AgeExt([DOB],[DOD]) 'calculate using DOB and DOD values.
After finally placing the code in the correct module with the correct code for calling the function, I think I've isolated the last annoying problem.
In correlation with my customized function code below:
1) The code will display #Error if neither date is supplied in the underlying table - the way it should, no problem
2) When using the call: =basDOB2AgeExt([DOB]), the code will display age from the date of birth (DOB) to the present time. Okay for some situations, but not my particular situation.
3) When using the call: =basDOB2AgeExt([DOB],[DOD]) there are two possible results:
a) If a date of death (DOD) *is* provided by the underlying table, age is nicely, precisely provided.
b) If no date of death (DOD) is provided by the underlying table, #Error is displayed in the control. Not so good.
I'm confused as to what the "If" code in the function is doing. To me, the code appears as if - if [DOD] = -1 (true), then it should = Date. But shouldn't it be if [DOD] = 0 (false), then it should = Date?
Any advice will be greatly appreciated.
Here's my customized code:
Public Function basDOB2AgeExt(DOB As Date, Optional DOD As Date = -1) As String
'Michael Red 5/23/02
'DOB is just the date of Birth
'DOD is an optional date to check - as in examples 1 & 2
'Otherwise, the DOB is checked against the Current Date
'? basDOB2AgeExt(#8/21/42#)
'59 Years 9 Months and 2 Days.
'? basDOB2AgeExt(#8/21/1942#, #8/20/2022#)
'79 Years 11 Months and 30 Days.
'? basDOB2AgeExt(#8/21/1942#, #8/21/2022#)
'80 Years 0 Months and 0 Days.
Dim tmpAge As Integer 'Simple DateDiff w/o Birthday correction
Dim tmpDt As Date 'Date to use in intermediate Calcs
Dim DtCorr As Boolean 'BirthDay Before or After date in question
Dim YrsAge As Integer
Dim MnthsAge As Integer 'Additional Mnths
Dim DaysAge As Integer 'Additional Days
If (DOD = -1) Then 'Check for (Optional Date to Check against)
DOD = Date 'If Not Supplied, Assume Today
End If
tmpAge = DateDiff("YYYY", DOB, DOD) 'Just the Years considering Jan 1, Mam
DtCorr = DateSerial(Year(DOD), Month(DOB), Day(DOB)) > DOD
YrsAge = tmpAge + DtCorr 'Just Years and Correction
tmpDt = DateAdd("yyyy", YrsAge, DOB)
MnthsAge = DateDiff("m", tmpDt, DOD)
DtCorr = DateAdd("m", MnthsAge, tmpDt) > DOD
MnthsAge = MnthsAge + DtCorr
tmpDt = DateAdd("m", MnthsAge, tmpDt)
DaysAge = DateDiff("d", tmpDt, DOD)
basDOB2AgeExt = YrsAge & " Years " & MnthsAge & " Months and " & DaysAge & " Days."
End Function
randy700 (Programmer)
Called by:
=basDOB2AgeExt([DOB]) 'no DOD value passed, use current date.
=basDOB2AgeExt([DOB],[DOD]) 'calculate using DOB and DOD values.