Sign In | Register Now About Bytes | Help | Site Map
Connecting Tech Pros Worldwide

Construct Projected Date

Question posted by: Dan2kx (Member) on May 14th, 2008 06:36 PM
Hello to all,

what i would like to do is have a bit of VB code to set a variable to the next tax year but am having trouble constructing it..

Taxdate = 01/04/year+1

so if this year 2008 then i want the taxt year to return the first monday around 01/04/2009
taking into account that if today was after new year the next taxdate would be the same... any ideas???

thanks for the interest
zepphead80's Avatar
zepphead80
Member
119 Posts
May 14th, 2008
07:04 PM
#2

Re: Construct Projected Date
Quote:
Hello to all,

what i would like to do is have a bit of VB code to set a variable to the next tax year but am having trouble constructing it..

Taxdate = 01/04/year+1

so if this year 2008 then i want the taxt year to return the first monday around 01/04/2009
taking into account that if today was after new year the next taxdate would be the same... any ideas???

thanks for the interest


Look in the Access help pages for the DateAdd function. It has three arguments: the time interval you are working with (e.g. days, months, etc.), how many of those intervals you want to add, and the date you want to add to.

For instance, to add 25 days to May 14th, 2008, you could write DateAdd("d", 25, "14-May-2008"). For you, the only twist is that the number of intervals would have to be calculated depending on what the current date is, which you could probably use DateDiff for...

Pat

Reply
Dan2kx's Avatar
Dan2kx
Member
125 Posts
May 14th, 2008
07:15 PM
#3

Re: Construct Projected Date
Its unfortunately the twist that i cant wrap my head around...

i would have to work out the date before i could work out the difference to work out the date, doesnt make sense does it is it possible to do something like this


if month(date) between 04 and12 then x = 1 else x = 0
taxdate = day(01)+month(04)+year(x)

Reply
zepphead80's Avatar
zepphead80
Member
119 Posts
May 14th, 2008
07:59 PM
#4

Re: Construct Projected Date
Quote:
Its unfortunately the twist that i cant wrap my head around...

i would have to work out the date before i could work out the difference to work out the date, doesnt make sense does it is it possible to do something like this


if month(date) between 04 and12 then x = 1 else x = 0
taxdate = day(01)+month(04)+year(x)


OK, try this:

Expand|Select|Wrap|Line Numbers
  1.  Dim intCurrentMonth As Integer 
  2. Dim intCurrentYear As Integer
  3. Dim intNextYear As Integer
  4.  
  5. Dim intNumOfDaysToAdd As Integer
  6.  
  7. Dim dteTaxDate As Date
  8.  
  9. intCurrentMonth = DatePart("m", Now)
  10. intCurrentYear = DatePart("yyyy", Now)
  11.  
  12. intNextYear = intCurrentYear+1
  13.  
  14. If intCurrentMonth >=4 And intCurrentMonth <=12 Then
  15.  
  16.      intNumOfDaysToAdd = DateDiff("d", Now, "1-April-" & intNextYear)     
  17.  
  18. Else
  19.  
  20.      intNumOfDaysToAdd = DateDiff("d", Now, "1-April-" & intCurrentYear)
  21.  
  22. EndIf
  23.  
  24. dteTaxDate = DateAdd("d", intNumOfDaysToAdd, Now)


This is actually quick and dirty, because I'm using the VB canned date functions, but it seems to work with the examples I tried out in my Immediate Window...

Pat

Last edited by zepphead80 : May 14th, 2008 at 08:05 PM. Reason: Improvement to code snippet...
Reply
Dan2kx's Avatar
Dan2kx
Member
125 Posts
May 14th, 2008
08:06 PM
#5

Re: Construct Projected Date
genius that seems to work thanks!!

Reply
zepphead80's Avatar
zepphead80
Member
119 Posts
May 14th, 2008
08:15 PM
#6

Re: Construct Projected Date
Quote:
genius that seems to work thanks!!


It's no problem. And one other thing...when I ran it in my Immediate Window, I found that it returned dteTaxDate with the time of day part also. You might want to take action to get rid of that, depending on what your need is.

Pat

Reply
Dan2kx's Avatar
Dan2kx
Member
125 Posts
May 14th, 2008
08:39 PM
#7

Re: Construct Projected Date
substituting "now" for "date" solved that problem

thanks again

Reply
NeoPa's Avatar
NeoPa
Administrator
11,040 Posts
May 16th, 2008
12:08 AM
#8

Re: Construct Projected Date
Expand|Select|Wrap|Line Numbers
  1. =CDate("1 April " & Year(Date())+IIf(Month(Date())<4,0,1))

Reply
zepphead80's Avatar
zepphead80
Member
119 Posts
May 16th, 2008
02:07 PM
#9

Re: Construct Projected Date
Quote:
Expand|Select|Wrap|Line Numbers
  1. =CDate("1 April " & Year(Date())+IIf(Month(Date())<4,0,1))


Now I'm eating my words "quick and dirty"...

Reply
NeoPa's Avatar
NeoPa
Administrator
11,040 Posts
May 16th, 2008
05:10 PM
#10

Re: Construct Projected Date
Not so fast Pat.

My solution doesn't find the Monday, it just finds the 1st of the month.

Reply
zepphead80's Avatar
zepphead80
Member
119 Posts
May 16th, 2008
06:22 PM
#11

Re: Construct Projected Date
Quote:
Not so fast Pat.

My solution doesn't find the Monday, it just finds the 1st of the month.


And neither does mine! Yikes...

This would find the first Monday after the tax date:

Expand|Select|Wrap|Line Numbers
  1.  Dim dteTaxDate As Date 
  2.  
  3. dteTaxDate=CDate("1 April " & Year(Date())+IIf(Month(Date())<4,0,1))
  4.  
  5. while DatePart("w",dteTaxDate) <> vbMonday
  6.      dteTaxDate = DateAdd("d", 1, dteTaxDate)
  7. wend


Pat

Reply
NeoPa's Avatar
NeoPa
Administrator
11,040 Posts
May 16th, 2008
07:13 PM
#12

Re: Construct Projected Date
You can use the Weekday() function arithmetically to go straight to the correct date value :
Expand|Select|Wrap|Line Numbers
  1. Public Function TaxDate() As Date
  2.   TaxDate = CDate("1 April " & Year(Date()) + IIf(Month(Date()) < 4, 0, 1))
  3.   TaxDate = TaxDate + 7 - Weekday(TaxDate, vbTuesday)
  4. End Function


PS. The reason this is difficult (more complicated) in SQL is that the 1st April calculation would need to appear twice.

Reply
zepphead80's Avatar
zepphead80
Member
119 Posts
May 16th, 2008
07:18 PM
#13

Re: Construct Projected Date
Quote:
And neither does mine! Yikes...

This would find the first Monday after the tax date:

Expand|Select|Wrap|Line Numbers
  1.  Dim dteTaxDate As Date 
  2.  
  3. dteTaxDate=CDate("1 April " & Year(Date())+IIf(Month(Date())<4,0,1))
  4.  
  5. while DatePart("w",dteTaxDate) <> vbMonday
  6.      dteTaxDate = DateAdd("d", 1, dteTaxDate)
  7. wend


Pat


I tried this out with several dates and it seems to work nicely, finding the nearest Monday to April 1st...

Expand|Select|Wrap|Line Numbers
  1.  Dim dteTaxDate As Date 
  2. Dim a As Integer
  3.  
  4. dteTaxDate = CDate("1 April " & Year(#12/6/2007#) + IIf(Month(#12/6/2007#) < 4, 0, 1))
  5.  
  6. a = DatePart("w", dteTaxDate, vbMonday) - 1
  7.  
  8. dteTaxDate = IIf(a <= 3, DateAdd("d", -a, dteTaxDate), DateAdd("d", 7 - a, dteTaxDate))


Pat

Reply
FishVal's Avatar
FishVal
Expert
1,895 Posts
May 16th, 2008
09:20 PM
#14

Re: Construct Projected Date
Hello, gentlemen.

It seems to me that the logic has a bug.
What will happen if current date is Sunday, 2 April?

Reply
NeoPa's Avatar
NeoPa
Administrator
11,040 Posts
May 16th, 2008
10:32 PM
#15

Re: Construct Projected Date
Then you get Monday 2nd April (2007).

I tested it (my code) for Sunday 2nd April 2006.

PS. Did you mean gentlemen? Or were you talking only of Pat's code?

Reply
FishVal's Avatar
FishVal
Expert
1,895 Posts
May 16th, 2008
11:12 PM
#16

Re: Construct Projected Date
Quote:
Then you get Monday 2nd April (2007).

I tested it (my code) for Sunday 2nd April 2006.


That is actually what I've meant. Don't you think Monday 3rd April 2006 would be the right answer?

Reply
NeoPa's Avatar
NeoPa
Administrator
11,040 Posts
May 16th, 2008
11:26 PM
#17

Re: Construct Projected Date
That makes a certain sense Fish, but from post #3, I would say no.

Reply
FishVal's Avatar
FishVal
Expert
1,895 Posts
May 17th, 2008
06:51 AM
#18

Re: Construct Projected Date
Quote:
That makes a certain sense Fish, but from post #3, I would say no.


Oh. Post #3 is not about "monday issue" at all. ;)
And, come on guys, don't you like this more challenging riddle?

Reply
NeoPa's Avatar
NeoPa
Administrator
11,040 Posts
May 19th, 2008
11:06 AM
#19

Re: Construct Projected Date
Quote:
Oh. Post #3 is not about "monday issue" at all. ;)

Not sure I'd agree about post #3 there Fish. It's the OP's request spelled out is all. It's all one request.
Quote:
And, come on guys, don't you like this more challenging riddle?

However, there may well have been confusion and, as a general rule, it's more fun dealing with the more complex formulae so we can look at it (the more challenging riddle) anyway.

I frankly can't see a way to subsume this new version into the existing logic, nor even produce new logic that incorporates this as a part of the whole. The best I can do at this time is to add a separate test at the front, which works fine, but isn't really very interesting I'm afraid.

If I can come up with a more unified (interesting) solution I will.

Reply
zepphead80's Avatar
zepphead80
Member
119 Posts
May 19th, 2008
06:48 PM
#20

Re: Construct Projected Date
Quote:
Hello, gentlemen.

It seems to me that the logic has a bug.
What will happen if current date is Sunday, 2 April?


I tested my code (well, really it's partly NeoPa's code also) in Post 13 with 4/2/2006 as the current date and it gives me 4/2/2007, which seems correct. Looking at Post 1, he wanted the closest Monday around April 1st, which 4/2/2007 satisfies...

The code that NeoPa wrote out with the IIf statement:

Expand|Select|Wrap|Line Numbers
  1. dteTaxDate = CDate("1 April " & Year(#4/2/2006#) + IIf(Month(#4/2/2006#) < 4, 0, 1))


will advance the year by 1 so long as the current date falls on or after 4/1.

Pat

Reply
NeoPa's Avatar
NeoPa
Administrator
11,040 Posts
May 19th, 2008
11:23 PM
#21

Re: Construct Projected Date
Pat, the point is that the succeeding 1st Monday of April would actually be Monday 3rd April 2006.

...If you assume that "any date before April" actually means "any date before the first Monday in April".

Reply
zepphead80's Avatar
zepphead80
Member
119 Posts
May 20th, 2008
02:54 PM
#22

Re: Construct Projected Date
Quote:
Pat, the point is that the succeeding 1st Monday of April would actually be Monday 3rd April 2006.

...If you assume that "any date before April" actually means "any date before the first Monday in April".


Yes, I understand that, but my impression from Dan2kx's posts is that it should advance to the next year, even with a date like 4/2/2006. But we'd need him to clarify that, and he probably stopped reading like 15 posts ago! :-)

Pat

Reply
NeoPa's Avatar
NeoPa
Administrator
11,040 Posts
May 20th, 2008
04:00 PM
#23

Re: Construct Projected Date
Absolutely right Pat ;)

Reply
Reply
Not the answer you were looking for? Post your question . . .
189,075 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Latest Articles: Read & Comment
Top Microsoft Access / VBA Forum Contributors