I need to calculate an age value but with a twist. I need to have anyone under 2 years to have a value in months with an “M” character attached. For everyone else just a year number is fine. Since I will have a DOB (Date of Birth) my thought is to do the calculation first in months, then everyone 24 or less add an “M” and then convert the others to years. That seems a bit convoluted. Any suggestions?
![]() |
Patch reliability is unclear. Unless you have an immediate, pressing need to install a specific patch, don't do it. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
-
Age Function (MS Access 2003)
Home » Forums » AskWoody support » Productivity software by function » MS Access and database help » Age Function (MS Access 2003)
- This topic has 5 replies, 4 voices, and was last updated 18 years, 11 months ago.
Viewing 1 reply threadAuthorReplies-
WSHansV
AskWoody LoungerMay 17, 2006 at 2:26 pm #1012549You could put this function in a module and use it in a query or in the control source of a text box:
Function ShowAge(ByVal varDOB) As String
Dim lngYears As Long
Dim lngMonths As Long
Dim lngWeeks As Long
Dim lngDays As Long
If IsNull(varDOB) Then
Exit Function
End If
If Not IsDate(varDOB) Then
Exit Function
End If
If Date < CDate(varDOB) Then
Exit Function
End If
varDOB = CDate(varDOB)
lngYears = Year(Date) – Year(varDOB)
If Month(Date) < Month(varDOB) Or Month(Date) = _
Month(varDOB) And Day(Date) 1 Then
ShowAge = lngYears
Else
lngMonths = DateDiff(“m”, varDOB, Date)
If Day(Date) < Day(varDOB) Then
lngMonths = lngMonths – 1
End If
ShowAge = lngMonths & "M"
End If
End FunctionUse it as
MyAge: ShowAge([DateOfBirth])
or
=ShowAge([DateOfBirth])
-
WSjaf90
AskWoody Lounger -
WSHansV
AskWoody Lounger -
WSjaf90
AskWoody Lounger -
WSMarkD
AskWoody LoungerMay 22, 2006 at 5:14 pm #1013168As noted you could use a query expression rather than user-defined function to perform this calculation, but the expression will need to be somewhat more convoluted. Attached db file (A2K format) shows an example, see query qry_GetAge, which uses either method to calculate age in years or months, or either depending on person’s age. Note that if calculating months only, you can format results to include an “M” using format string “#M” (the backslash “escapes” the “M” as a literal), and still use the resulting numbers in calculations (see Age Months (Formatted)); but when combining years (number) and months (formatted text) in same calculated field (Age Years Or Months (Expr)), only the numerical results will be usable in calculation, as shown by Test Calc column. Do not know of a way to apply more than one format to a single field in query. This is expression I used:
Age Years Or Months (Expr): IIf(IIf(Date()24,IIf(Date()<DateSerial(Year(Date()),Month([BirthDate]),Day([BirthDate])),DateDiff("yyyy",[BirthDate],Date())-1,DateDiff("yyyy",[BirthDate],Date())),Format(IIf(Date()<DateSerial(Year(Date()),Month(Date()),Day([BirthDate])),DateDiff("m",[BirthDate],Date())-1,DateDiff("m",[BirthDate],Date())),"#M"))
The db uses copy of Northwind “Employees” table (with some dummy records added) to test query. Generally a query that uses expressions using only built-in VBA functions will open quicker that equivalent query that uses user-defined VBA functions. The logic is easier to follow in a function, but with small sets of records the difference in query execution time will be negligible.
HTH
-
-
-
Viewing 1 reply thread -

Plus Membership
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Get Plus!
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Search Newsletters
Search Forums
View the Forum
Search for Topics
Recent Topics
-
Two blank icons
by
CR2
1 hour, 55 minutes ago -
Documents, Pictures, Desktop on OneDrive in Windows 11
by
ThePhoenix
4 hours, 19 minutes ago -
End of 10
by
Alex5723
7 hours ago -
End Of 10 : Move to Linux
by
Alex5723
7 hours, 29 minutes ago -
Single account cannot access printer’s automatic duplex functionality
by
Bruce
3 hours, 58 minutes ago -
test post
by
gtd12345
13 hours ago -
Privacy and the Real ID
by
Susan Bradley
3 hours, 8 minutes ago -
MS-DEFCON 2: Deferring that upgrade
by
Susan Bradley
3 hours, 57 minutes ago -
Cant log on to oldergeeks.Com
by
WSJonharnew
17 hours, 22 minutes ago -
Upgrading from Win 10
by
WSjcgc50
4 hours, 46 minutes ago -
USB webcam / microphone missing after KB5050009 update
by
WSlloydkuhnle
12 hours, 27 minutes ago -
TeleMessage, a modified Signal clone used by US government has been hacked
by
Alex5723
1 day, 9 hours ago -
The story of Windows Longhorn
by
Cybertooth
20 hours, 46 minutes ago -
Red x next to folder on OneDrive iPadOS
by
dmt_3904
1 day, 10 hours ago -
Are manuals extinct?
by
Susan Bradley
7 hours, 15 minutes ago -
Canonical ditching Sudo for Rust Sudo -rs starting with Ubuntu
by
Alex5723
1 day, 20 hours ago -
Network Issue
by
Casey H
1 day, 7 hours ago -
Fedora Linux is now an official WSL distro
by
Alex5723
2 days, 8 hours ago -
May 2025 Office non-Security updates
by
PKCano
2 days, 8 hours ago -
Windows 10 filehistory including onedrive folder
by
Steve Bondy
2 days, 10 hours ago -
pages print on restart (Win 11 23H2)
by
cyraxote
1 day, 11 hours ago -
Windows 11 Insider Preview build 26200.5581 released to DEV
by
joep517
2 days, 12 hours ago -
Windows 11 Insider Preview build 26120.3950 (24H2) released to BETA
by
joep517
2 days, 12 hours ago -
Proton to drop prices after ruling against “Apple tax”
by
Cybertooth
2 days, 20 hours ago -
24H2 Installer – don’t see Option for non destructive install
by
JP
1 day, 12 hours ago -
Asking Again here (New User and Fast change only backups)
by
thymej
3 days, 7 hours ago -
How much I spent on the Mac mini
by
Will Fastie
14 hours, 49 minutes ago -
How to get rid of Copilot in Microsoft 365
by
Lance Whitney
1 day, 10 hours ago -
Spring cleanup — 2025
by
Deanna McElveen
3 days, 13 hours ago -
Setting up Windows 11
by
Susan Bradley
2 days, 8 hours ago
Recent blog posts
Key Links
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.