ACC: Two Functions to Calculate Age in Months and YearsLast reviewed: August 29, 1997Article ID: Q100136 |
The information in this article applies to:
SUMMARYAdvanced: Requires expert coding, interoperability, and multiuser skills. This article explains how to create a function to calculate the age of a person or thing based on a given date. This article assumes that you are familiar with Visual Basic for Applications and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information about Visual Basic for Applications, please refer to your version of the "Building Applications with Microsoft Access" manual. NOTE: Visual Basic for Applications is called Access Basic in Microsoft Access versions 1.x and 2.0. For more information about Access Basic, please refer to the "Introduction to Programming" manual in Microsoft Access version 1.x or the "Building Applications" manual in Microsoft Access version 2.0
MORE INFORMATIONEnter the following code in a module: NOTE: In the following sample code, an underscore (_) at the end of a line is used as a line-continuation character. Remove the underscore from the end of the line when re-creating this code in Access Basic.
'========================================================== ' General Declaration '========================================================== Option Explicit '************************************************************* ' FUNCTION NAME: Age() ' ' PURPOSE: ' Calculates age in years from a given 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 given date. ' If the given 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 String) 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 Testing the Age() and AgeMonths() FunctionsThe following steps describe how to test the Age() and AgeMonths() functions:
Using the Age() and AgeMonths() FunctionsThe following procedure explains how to mark old orders by placing the age value in a new control:
REFERENCESFor more information about date differences, search the Help Index for "DateDiff," or ask the Microsoft Access 97 Office Assistant.
|
Additional query words: birth birthday birthdate age abc elapsed time
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |