I have created a calculated field in a query (modified from a FAQ: faq181-55) which works great, calculating from the present date as well as a date of death - but it's not in the format I desire; e.g. 9 months = 0.666666666666667 or .67.
CalcAge: IIf(IsNull([DOD]),Fix(DateDiff("m",[DOB],Now())+Int(Format(Now(),"mmdd")<Format([DoB],"mmdd")))/12,Fix(DateDiff("m",[DOB],[DOD])+Int(Format([DOD],"mmdd")<Format([DOB],"mmdd")))/12)
So I tried the following function as a starting point to get closer to the format I'm looking for (years, months) (ref:Option Compare Database
Option Explicit
'*************************************************************
' FUNCTION NAME: Age()
'
' PURPOSE:
' Calculates age in years from a specified date to today's date.
'
' INPUT PARAMETERS:
' StartDate: The beginning date (for example, a birth date).
'
' RETURN
' Age in years.
'
'*************************************************************
Function Age(varBirthDate As Variant) As Integer
Dim varAge As Variant
If IsNull(varBirthDate) Then Age = 0: Exit Function
varAge = DateDiff("yyyy", varBirthDate, Now)
If Date < DateSerial(Year(Now), Month(varBirthDate), _
Day(varBirthDate)) Then
varAge = varAge - 1
End If
Age = CInt(varAge)
End Function
'*************************************************************
' FUNCTION NAME: AgeMonths()
'
' PURPOSE:
' Compliments the Age() function by calculating the number of months
' that have expired since the last month supplied by the specified date.
' If the specified date is a birthday, the function returns the number of
' months since the last birthday.
'
' INPUT PARAMETERS:
' StartDate: The beginning date (for example, a birthday).
'
' RETURN
' Months since the last birthday.
'*************************************************************
Function AgeMonths(ByVal StartDate As Variant) As Integer
Dim tAge As Double
tAge = (DateDiff("m", StartDate, Now))
If (DatePart("d", StartDate) > DatePart("d", Now)) Then
tAge = tAge - 1
End If
If tAge < 0 Then
tAge = tAge + 1
End If
AgeMonths = CInt(tAge Mod 12)
End Function
But when I invoke the function, using =Age([DOB]) & " yrs " & AgeMonths([DOB]) & " mos" as the control source of an unbound text box in a form, the control displays #Error - although the control source is accepted without error when I close the properties box. I've checked for circular references, repeat of field names, field size, but nothing seems to solve the problem.
Can someone please tell me what I'm doing wrong?
Thanks in advance.
Using Access 2003, (11.8166.8221) SP3
CalcAge: IIf(IsNull([DOD]),Fix(DateDiff("m",[DOB],Now())+Int(Format(Now(),"mmdd")<Format([DoB],"mmdd")))/12,Fix(DateDiff("m",[DOB],[DOD])+Int(Format([DOD],"mmdd")<Format([DOB],"mmdd")))/12)
So I tried the following function as a starting point to get closer to the format I'm looking for (years, months) (ref:Option Compare Database
Option Explicit
'*************************************************************
' FUNCTION NAME: Age()
'
' PURPOSE:
' Calculates age in years from a specified date to today's date.
'
' INPUT PARAMETERS:
' StartDate: The beginning date (for example, a birth date).
'
' RETURN
' Age in years.
'
'*************************************************************
Function Age(varBirthDate As Variant) As Integer
Dim varAge As Variant
If IsNull(varBirthDate) Then Age = 0: Exit Function
varAge = DateDiff("yyyy", varBirthDate, Now)
If Date < DateSerial(Year(Now), Month(varBirthDate), _
Day(varBirthDate)) Then
varAge = varAge - 1
End If
Age = CInt(varAge)
End Function
'*************************************************************
' FUNCTION NAME: AgeMonths()
'
' PURPOSE:
' Compliments the Age() function by calculating the number of months
' that have expired since the last month supplied by the specified date.
' If the specified date is a birthday, the function returns the number of
' months since the last birthday.
'
' INPUT PARAMETERS:
' StartDate: The beginning date (for example, a birthday).
'
' RETURN
' Months since the last birthday.
'*************************************************************
Function AgeMonths(ByVal StartDate As Variant) As Integer
Dim tAge As Double
tAge = (DateDiff("m", StartDate, Now))
If (DatePart("d", StartDate) > DatePart("d", Now)) Then
tAge = tAge - 1
End If
If tAge < 0 Then
tAge = tAge + 1
End If
AgeMonths = CInt(tAge Mod 12)
End Function
But when I invoke the function, using =Age([DOB]) & " yrs " & AgeMonths([DOB]) & " mos" as the control source of an unbound text box in a form, the control displays #Error - although the control source is accepted without error when I close the properties box. I've checked for circular references, repeat of field names, field size, but nothing seems to solve the problem.
Can someone please tell me what I'm doing wrong?
Thanks in advance.
Using Access 2003, (11.8166.8221) SP3