chris.mo
11-10-2001, 08:25 AM
Please can someone tell me an expression to show an age field in a query from the DOB (short date) field in a table. Just in Yrs would do but yrs/months would be nice.
Thanks.
Chris.
Thanks.
Chris.
|
View Full Version : DOB - Age in Yrs. Months. chris.mo 11-10-2001, 08:25 AM Please can someone tell me an expression to show an age field in a query from the DOB (short date) field in a table. Just in Yrs would do but yrs/months would be nice. Thanks. Chris. R. Hicks 11-10-2001, 08:36 AM Here is a function that will give you the age in Years, Months, and Days. If you only want the Years and Months, reply back and I will alter the function for you if you can not do this yourself. Function GetAgeYMD(DOB As Variant) As String Dim intYears As Integer, intMonths As Integer, intDays As Integer Dim strTmpString As String If Not IsDate(DOB) Then Exit Function intMonths = Int(DateDiff("m", DOB, Date)) intYears = Int(intMonths / 12) intDays = DateDiff("d", DateAdd("m", intMonths, DOB), Date) intMonths = Int(intMonths Mod 12) If intDays < 0 Then   intMonths = intMonths - 1   intDays = DateDiff("d", DateAdd("m", -1, Date) - intDays, Date) End If If intMonths < 0 Then   intYears = intYears + intMonths   intMonths = 12 + intMonths End If If intYears = 1 Then   strTmpString = intYears & " Year " Else   strTmpString = intYears & " Years " End If If intMonths = 1 Then   strTmpString = strTmpString & intMonths & " Month " Else   strTmpString = strTmpString & intMonths & " Months " End If If intDays = 1 Then   strTmpString = strTmpString & intDays & " Day " Else   strTmpString = strTmpString & intDays & " Days " End If GetAgeYMD = strTmpString End Function HTH RDH chris.mo 11-11-2001, 07:13 AM Wow! Thanks a lot Mr. Hicks, your response is much appreciated. However, I think we must have our wires crossed. I had had some success with this... Expr1: (Date()-[Goalkeepers]![Date of birth])/365.33 ...Simply subtracting the DOB date field from the current date then dividing by 365.33. This gives the right age, but with a load of decimal places I cant get rid of even by setting the decimal places to 0 in the format property box?? I might be just ignorant here but isnt there a shorter way than you suggest? I dont wish to appear ungrateful but im a bit cconfused. Chris. |