473,471 Members | 1,995 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Calculating next x anniversary dates

6 New Member
I need to query a table about next x anniversary dates, based on birthdate.
Can someone teel me how to do it?
Thanks.
Nov 24 '09 #1

✓ answered by Stewart Ross

Hi. The anniversary of a birthdate is just the month and day of the birthdate with the current year (if the anniversary has not already occurred this year) or the next year substituted in place of the year of birth. The DateSerial function can be used for this, and has the advantage that it copes with leap year anniversaries of 29 Feb without further adjustment. Assuming table called tblDOB and field called DOB which stores dates of birth:

Expand|Select|Wrap|Line Numbers
  1. SELECT DOB, 
  2.        DateSerial(Year(Date()) + IIf(Month([DOB])*100+Day([DOB])<=Month(Date())*100+Day(Date()),1,0),
  3.                   Month([DOB]),
  4.                   Day([DOB])) AS Anniversary
  5. FROM tblDOB;
  6.  
Test Data

Expand|Select|Wrap|Line Numbers
  1. DOB          Anniversary
  2. 25/06/1957   25/06/2010
  3. 25/12/1967   25/12/2009
  4. 01/01/2009   01/01/2010
  5. 29/02/2008   01/03/2010
  6. 14/06/1986   14/06/2010
  7. 14/12/2007   14/12/2009
  8. 08/12/2007   08/12/2010
  9.  
The IIF expression in the DateSerial part of the formula just adds 1 to the current year if the month and day of the given birthdate have already passed.

-Stewart

17 9735
topher23
234 Recognized Expert New Member
I'm not sure what you mean by anniversaries based on birthdates. Can you clarify that?
Nov 25 '09 #2
apsf68
6 New Member
Ok.
I have a table with a lot of users, and I want to querie that table to know who are the usersthat will have her birthday in the next x days.
My english isn't very good, but I hope it's good enought to let you understand what I need.
Thanks.
Nov 25 '09 #3
topher23
234 Recognized Expert New Member
Okay, what you're looking for here is a select query with some criteria. Not knowing the names of your table fields, I'll just mock someting up:

Expand|Select|Wrap|Line Numbers
  1. SELECT UserName, UserBirthday FROM tblUsers 
  2. WHERE UserBirthday Between Date() And DateAdd("d",7,Date());
This SQL statement creates a query that selects the UserName and UserBirthday fields from a table called tblUsers, and only shows the users who have birthdays between today and 7 days from today.

To use a field on a form to do this, I'll reference a form called frmBirthdays with a text box called txtEndDate.

Expand|Select|Wrap|Line Numbers
  1.  SELECT UserName, UserBirthday FROM tblUsers 
  2. WHERE UserBirthday Between Date() And Forms!frmBirthdays!txtEndDate;
Now, these are SQL statements. These are what are created when you use the Query Designer. So, in the query designer, you would select your users table, drag in your username and birthday fields, then type 'Between Date() And ...(whichever method you plan to use). You can right-click your query and look at it in SQL View to see the similarity with what I've typed above.

Once you have your query built, you can use it as the record source for a report, a list box, etc.
Nov 25 '09 #4
OldBirdman
675 Contributor
An anniversary, or a birthday, is a day, NOT a date. An anniversary would be a day in a year. So the 365th day is 31 December most of the time, but 30 December in a Leap Year. I believe this is referred to as a 'Date Serial' in Access.
Most databases store the birth date, say 31 December, 1966. We would have to see if this day, not date, for the year 2009, is within 'x' days from now. As today is 25 November, 2009, x would have to be >=35 in order for this to be true.
But if x is greater than 35, we would be looking for birthdays in January also, which would have date serial values lower than today's date serial value.
Expand|Select|Wrap|Line Numbers
  1. DateSerial = Format(DateToConvert, "y")
I do not know how to do this as an SQL solution. Even writing a function isn't that simple because of leap year, even if we ignore the fact that the year 2100 will not be a leap year even though Mod(2100, 4) = 0, which normally denotes a leap year.
Nov 26 '09 #5
topher23
234 Recognized Expert New Member
I see your point, OB. I need to rethink this with a solution that will work. I don't know what I was thinking earlier.
Nov 26 '09 #6
topher23
234 Recognized Expert New Member
I've got it this time. This is a very convoluted function that converts the date to a string and appends the current year. Messing with the date functions just caused more trouble than it solved, as OldBirdman pointed out. So now, instead of using the actual UserBirthday field, you'll create a new calculated field, as follows:

Expand|Select|Wrap|Line Numbers
  1. expAnniversary: CDate(Left(CStr([UserBirthday]),InstrRev(CStr([UserBirthday]),"/")) & Year(Now()))
Apply the same criteria as posted earlier to this new calculated field and it will work.
Nov 26 '09 #7
NeoPa
32,556 Recognized Expert Moderator MVP
OB,

The formula for determining Leap Years is a little more involved than that.

A year is Leap if it is divisible by 4, unless it is divisible by 100. Even then, if it is divisible by 400 it is also a Leap year.

The year 2000 was just such an example of the double exception.
Nov 30 '09 #8
NeoPa
32,556 Recognized Expert Moderator MVP
Topher,

This is nearly there, but it will fail when X extends beyond the current year. I'll let you have a stab at this, but it involves checking if the day (within the year) of the anniversary is less than the day of Date().
Nov 30 '09 #9
topher23
234 Recognized Expert New Member
I realized that this wouldn't always work as I was looking over this thread this morning. The previous solution was put together hastily as I was on the way out the door for the long US holiday weekend, so I didn't think to account for new year rollovers.

And of course, you (NeoPa) hit the nail right on the head with the way to make this work. apsf68, you'll need to use an IIf statement in your calculated field. If you've never used one of these before, read about it in the VBA help file - it's a very useful function.

This will now create a calculated field using the IIf function to see if the birthday month is less than the current month. If it is, it will append next year's Year to the birth date rather than the current year. This is the most compact method I could come up with for doing this. I'd also hazard a guess that this is pretty much the same logic most of us use to calculate the same thing in our heads.

Expand|Select|Wrap|Line Numbers
  1. expAnniversary: 
  2. IIf(CDate(Month([UserBirthday])<Month(Now()),
  3. CDate(Left(CStr([UserBirthday]),InstrRev(CStr([UserBirthday]),"/")) & Year(DateAdd("yyyy",1,Now()))),
  4. CDate(Left(CStr([UserBirthday]),InstrRev(CStr([UserBirthday]),"/")) & Year(Now())))
Obviously, this won't account for birthdays that occurred earlier in the month on a list for next year's birthdays. If you intend to look that far out, I'm sure you can figure out how to modify this code to go down to the date level rather than the month level by checking the calculated date for this year against the current date. Just note that the more functions you use, the slower your query will run.
Nov 30 '09 #10
OldBirdman
675 Contributor
NeoPa,
I knew that. I entered this thread to point up the difference between a date and an (anniversary) day. The stated problem is to see what occurances in the future exist within an entered range: today to/thru the entered date.
I didn't want to complicate it too much, but wanted to point out that the y2K+100 (y2.1k?) date was an exception. If this code is still running in the year 2200, apsf68 deserves an award for something!
Nov 30 '09 #11
NeoPa
32,556 Recognized Expert Moderator MVP
Fair enough OB. It never hurts to drop these tidbits of info around though I find ;)

y2.1k - What are you like :D
Nov 30 '09 #12
apsf68
6 New Member
Hi everybody,

First of all, thank you very much for your help.
I'm working on a solution I found after reading all your posts.
As soon as I have it developed, I will post it in order to all can use it if someone find it useful.
Dec 7 '09 #13
apsf68
6 New Member
Hi,

This is how I did it:
Expand|Select|Wrap|Line Numbers
  1. expAnniversary: IIf(CDate(Day([UserBirthday]) & "-" &
  2.                     Month([UserBirthday]) & "-" &
  3.                     Year(Now()))<Date();
  4.                     CDate(Day([UserBirthday]) & "-" &
  5.                     Month([UserBirthday]) & "-" &
  6.                     Year(Now())+1);
  7.                     CDate(Day([UserBirthday]) & "-" & 
  8.                     Month([UserBirthday]) & "-" & 
  9.                     Year(Now())))
Once I have it, is possible to know who is having is anniversary on the next x days.
Hope it could help someone.
Dec 8 '09 #14
Stewart Ross
2,545 Recognized Expert Moderator Specialist
Hi. The anniversary of a birthdate is just the month and day of the birthdate with the current year (if the anniversary has not already occurred this year) or the next year substituted in place of the year of birth. The DateSerial function can be used for this, and has the advantage that it copes with leap year anniversaries of 29 Feb without further adjustment. Assuming table called tblDOB and field called DOB which stores dates of birth:

Expand|Select|Wrap|Line Numbers
  1. SELECT DOB, 
  2.        DateSerial(Year(Date()) + IIf(Month([DOB])*100+Day([DOB])<=Month(Date())*100+Day(Date()),1,0),
  3.                   Month([DOB]),
  4.                   Day([DOB])) AS Anniversary
  5. FROM tblDOB;
  6.  
Test Data

Expand|Select|Wrap|Line Numbers
  1. DOB          Anniversary
  2. 25/06/1957   25/06/2010
  3. 25/12/1967   25/12/2009
  4. 01/01/2009   01/01/2010
  5. 29/02/2008   01/03/2010
  6. 14/06/1986   14/06/2010
  7. 14/12/2007   14/12/2009
  8. 08/12/2007   08/12/2010
  9.  
The IIF expression in the DateSerial part of the formula just adds 1 to the current year if the month and day of the given birthdate have already passed.

-Stewart
Dec 8 '09 #15
NeoPa
32,556 Recognized Expert Moderator MVP
I've no idea who set the best answer to post #14, but as it's clearly a long way from being so I've reset it.

I would have selected one earlier but there are a number of good answers with some good techniques illustrated.

As Stewart's seems now to be the most succinct I will set that one, but I would certainly comment that Topher's post #10 deserves an Honourable Mention.
Dec 9 '09 #16
topher23
234 Recognized Expert New Member
I've never seen a practial use of DateSerial before - that's quite elegant. Thanks, Stewart!
Dec 9 '09 #17
NeoPa
32,556 Recognized Expert Moderator MVP
A new question (Hijack) was posted in here but has now been moved to Calculating Anniversary Date.
Apr 13 '19 #18

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

Similar topics

2
by: Richard Oliver | last post by:
Access 97----Have searched the FAQ sites with no success. I need a dbase to keep a record of maintenance calls and plan the next call . The maintenance periods run in monthly cycles which vary...
7
by: JLM | last post by:
I have a table that has fieldA, fieldB, fieldC. I want fieldC=fieldA-fieldB. simple enough. the next record I want to be able to do the same on the new value of fieldC. I can do this with SAP...
2
by: bufbec | last post by:
I have worked on this for hours and can't come up with a solution. Hope someone can help me. I have a table called TMBS_HMAUDIT_PARMS. this table contains data to tell me how often a person is...
3
by: Wired Hosting News | last post by:
Lets say I have 10 products in 10 different stores and every week I get a report from each store telling me how many items they have left for each of the 10 products. So each week I enter in 100...
1
by: b.beeching | last post by:
Not sure if my subject is entirely accurate but here goes. I need to calculate the date difference between a date A and a date B... however date B relies entily on date A. EG: i have an advert...
9
by: mankolele | last post by:
Hi all I need an idea on where to start when calculating a total amount betwee n two dates from a database like I want amount of money paid out from 30/03/2004 and 30/07/2004 ,but in the database...
3
by: mlcampeau | last post by:
Hi guys, I am trying to run a query that calculates when employees become eligible for supplemental vacation. This occcurs once the employee has reached 5, 10, 15, etc years of service. I got the...
2
by: ncsthbell | last post by:
I am having problems getting the end date to calculate correctly. I start with Quarter '03/02', (YY/QTR), for this it means it is for the 2nd qtr of 2003. My goal is to get the begin & end dates...
8
by: =?Utf-8?B?QWw=?= | last post by:
I am working in vb2005. how can I calculate business days (not including holidays and weekends) between 2 dates? thanks Al
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.