473,395 Members | 1,532 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

calculating age from DOB and adding to table via a query

simple age calculation from DOB and adding to table via a query
May 11 '20 #1

✓ answered by cactusdata

The function:

Expand|Select|Wrap|Line Numbers
  1. Public Function AgeSimple( _
  2.   ByVal datDateOfBirth As Date) _
  3.   As Integer
  4.  
  5. ' Returns the difference in full years from datDateOfBirth to current date.
  6. '
  7. ' Calculates correctly for:
  8. '   leap years
  9. '   dates of 29. February
  10. '   date/time values with embedded time values
  11. '
  12. ' DateAdd() is used for check for month end of February as it correctly
  13. ' returns Feb. 28. when adding a count of years to dates of Feb. 29.
  14. ' when the resulting year is a common year.
  15. ' After an idea of Markus G. Fischer.
  16. '
  17. ' 2007-06-26. Cactus Data ApS, CPH.
  18.  
  19.   Dim datToday  As Date
  20.   Dim intAge    As Integer
  21.   Dim intYears  As Integer
  22.  
  23.   datToday = Date
  24.   ' Find difference in calendar years.
  25.   intYears = DateDiff("yyyy", datDateOfBirth, datToday)
  26.   If intYears > 0 Then
  27.     ' Decrease by 1 if current date is earlier than birthday of current year
  28.     ' using DateDiff to ignore a time portion of datDateOfBirth.
  29.     intAge = intYears - Abs(DateDiff("d", datToday, DateAdd("yyyy", intYears, datDateOfBirth)) > 0)
  30.   End If
  31.  
  32.   AgeSimple = intAge
  33.  
  34. End Function
The query:

Expand|Select|Wrap|Line Numbers
  1. Select *, AgeSimple([DOB]) As Age
  2. From YourTable
Don't "add" an age to a table; calculate it when needed using the query.

1 2265
cactusdata
214 Expert 128KB
The function:

Expand|Select|Wrap|Line Numbers
  1. Public Function AgeSimple( _
  2.   ByVal datDateOfBirth As Date) _
  3.   As Integer
  4.  
  5. ' Returns the difference in full years from datDateOfBirth to current date.
  6. '
  7. ' Calculates correctly for:
  8. '   leap years
  9. '   dates of 29. February
  10. '   date/time values with embedded time values
  11. '
  12. ' DateAdd() is used for check for month end of February as it correctly
  13. ' returns Feb. 28. when adding a count of years to dates of Feb. 29.
  14. ' when the resulting year is a common year.
  15. ' After an idea of Markus G. Fischer.
  16. '
  17. ' 2007-06-26. Cactus Data ApS, CPH.
  18.  
  19.   Dim datToday  As Date
  20.   Dim intAge    As Integer
  21.   Dim intYears  As Integer
  22.  
  23.   datToday = Date
  24.   ' Find difference in calendar years.
  25.   intYears = DateDiff("yyyy", datDateOfBirth, datToday)
  26.   If intYears > 0 Then
  27.     ' Decrease by 1 if current date is earlier than birthday of current year
  28.     ' using DateDiff to ignore a time portion of datDateOfBirth.
  29.     intAge = intYears - Abs(DateDiff("d", datToday, DateAdd("yyyy", intYears, datDateOfBirth)) > 0)
  30.   End If
  31.  
  32.   AgeSimple = intAge
  33.  
  34. End Function
The query:

Expand|Select|Wrap|Line Numbers
  1. Select *, AgeSimple([DOB]) As Age
  2. From YourTable
Don't "add" an age to a table; calculate it when needed using the query.
May 11 '20 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Ralph Freshour | last post by:
I'm not sure the follow multiple table query is the right way to do what I need to do although it seems to be working: $php_SQL = "SELECT * ". "FROM basics, personal, photos ". "WHERE...
0
by: Gianfranco | last post by:
Hi I got a problem with 2 tables. I have a table, say A, with x records, coming from a make table query and a table, say B, with y records, coming from another make table query. I need to join...
3
by: Rich N | last post by:
I want to move several records from one table to a new table. My database is set up so that there's the "regular" database, and the ".be" database. In which do I use the Make Table Query?
1
by: PMB | last post by:
Thank you in advance for any and all assistance. I'm trying to use a make table query to pull the last transactionID, so I can use an append query to reset the transactionID to the next...
4
by: Oreo Bomb | last post by:
I have a secured database that contains a Read-Only group. This group has permissions to view reports, but cannot add, edit, or delete any DB objects. One of the reports the group needs access to...
2
by: Kathy Krizl | last post by:
I'm probably doing something stupid, but I have a make table query. One of the tables I reference has some check box fields in it. Their Data Type is Yes/No, their field property format is Yes/No,...
2
by: Dragon | last post by:
When I run a make-table query, the results take over 1 minute to return. The query is making a table that has 12,000 records in it. If I run the make-table query ONLY to see the records that will...
4
by: ken | last post by:
Hi, I use this command to run a make table query without opening it... CurrentDb.Execute "make table query name" Access tells me that it can't execute a select query...? Its a make table query...
3
by: Robertf987 | last post by:
Hi, I'm a bit stuck with an access database. I'm using access 2000 if that's any help. Right, it's 3:40am right now and I'm rather tired, but I *hope* this makes sense. I have a table which...
1
by: Mihail | last post by:
Hi all ! What I have: In a BE database I use a temporary table to store some data. Of course, for different filters that data will be different. Keep in mind, please, that the table structure is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.