Professional Documents
Culture Documents
Title Explanation
How This sample includes two Visual Basic functions that help you calculate an age based on a date that you enter in a table.
One of the funtions shows the number of years expired from today's date. The other function shows the number of months
to expired between today's date and the number of years expired. You can cut and paste either or both functions into a module
create in your own database.
two In the Navigation Pane on the left double click on the module, basAge, to view the Age() and the AgeMonths() functions.
functi The Age function returns the number of years between Now() and the name of the date field that you enclose in the Age()
ons to function.
specify.
The AgeMonths() function calculates the number of years and months that have expired since the date you
calcul
ate To see how the Age function works, click the View menu in the Visual Basic window while you are looking at the Age
function, and click Immediate Window. (You can also type Ctrl+G to open the Immediate window.)
age in
mont In the Immediate window type: ?Age(#11/15/67#) and press Enter to see the age in years.
In the Immediate window type: ?AgeMonths(#11/15/67#) and press Enter to see the age in years and months.
hs
and in Go back to the the Contact List. In the Property sheet in design view of the form, you will see that the Record Source for
years the form is the Contacts Extended query. The Contacts Extended query contains an expression named “ContactAge,”
which calls, or references, the function, Age(). It also contains an expression named “ContactAgeWithMonth,” which calls
the function, Age() as well as the AgeMonths() function.
Open the query Contacts Extended in design view to see the ContactAge expression.
Click on the ContactAge expression in the field row.
Right click and select Zoom to see the whole expression in the Zoom window:
ContactAge: Age([Birth Date])
Right click on the ContactAgeWithMonth expression in the Field row and select Zoom to see the whole expression:
ContactAgeWithMonth: Age([Birth Date]) & " years, " & AgeMonths([Birth Date]) &
" months"
The ContactAgeWithMonth expression uses an ampersand (&) to concatenate, or connect, the return value from the Age
function and the return value from the AgeMonths function along with the text for "years" and "months."
Note that each time you open the form, the calculation for both the fields, ContactAge and ContactAgeWithMonth occurs.
If you have several thousand records, the calculation of all records may affect the performance of the form. If you notice a
decrease in performance, do not include the calculation as part of the record source of the form and simply run a query to
see the age with a click of a button. To achieve that goal:
1. In the sample database template Northwind2007.accdb, type or copy the Age() and AgeMonth() functions in a new
module.
2. Open the Orders form in Design view and add an unbound text box control directly under the Order Date text box
control.
3. Type the following line in the ControlSource property of the new text box control:
=Age([Order Date]) & " yrs " & AgeMonths([Order Date]) & " mos"
4. View the form in Form view. Note that the age of the order is displayed in the new text box control.