Two functions to calculate age in months and years

This article was previously published under Q210522
This article has been archived. It is offered "as is" and will no longer be updated.
Advanced: Requires expert coding, interoperability, and multiuser skills.

This article applies to a Microsoft Access database (.mdb) and to a Microsoft Access project (.adp).

For a Access 2002 version of this article, see 290190.
This article shows you how to create two functions to calculate the age of a person or thing based on a specified date.

NOTE: You can see a demonstration of the technique that is used in this article in the sample file Qrysmp00.exe. For information about how to obtain this sample file, please see the following article in the MicrosoftKnowledge Base:
207626 The "Access 2000 sample: query topics" database available in Download Center

Creating the functions

Type the following code in a module:
'==========================================================' General Declaration'==========================================================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				

Testing the Age() and AgeMonths() functions

To test the Age() and AgeMonths() functions, follow these steps.

IMPORTANT: The following steps ask you to change the date on your computer. Make sure that you complete step 6 to reset the date to the current date.
  1. By using the Date/Time tool in Control Panel, make a note of the current date, and then set the date to June 3, 2000.
  2. Open a module or create a new one.
  3. On the View menu, click Immediate Window.
  4. Assume your friend's birth date was November 15, 1967 and today isJune 3, 2000. Type the following line in the Immediate window, and then press ENTER:
    Note that Microsoft Access responds with the value 32 (years).
  5. Type the following line, and then press ENTER:
    Note that Microsoft Access responds with the value 6, indicating that six months have passed since this person's last birthday. Your friend is 32 years and six months old.
  6. By using the Date/Time tool in Control Panel, reset the date to the current date that you noted in step 1.

Using the Age() and AgeMonths() Functions

The following procedure explains how to mark old orders by placing the agevalue in a new control.
  1. In the sample database Northwind.mdb, type the Age() and AgeMonth() functions in a new module.
  2. Open the Orders form in Design view and add an unbound text box control.
  3. Type the following line in the ControlSource property of the new text box control:
    =Age([OrderDate]) & " yrs " & AgeMonths([OrderDate]) & " mos"
  4. View the form in Form view. Note that the age of the order is displayed in the new text box control.
For more information about date differences, click Microsoft Access Help on the Help menu, type datediff function example in the Office Assistant or the Answer Wizard, and then click Search to view the topic.
birth birthday birthdate age abc elapsed time Qrysmp00 exe

Article ID: 210522 - Last Review: 12/05/2015 11:33:24 - Revision: 3.0

Microsoft Access 2000 Standard Edition

  • kbnosurvey kbarchive kbhowto kbprogramming KB210522