Date - calculate the next Wednesday (1 Viewer)

DavidCon

Registered User.
Local time
Yesterday, 19:02
Joined
Apr 14, 2011
Messages
23
Hi All,

I'm trying to calculate the next Wednesday on the field [due] i.e. today being 21/02/17, the next Wednesday will be 27/12/2017.

I have a working formula in excel which I'm trying to convert to access.

The excel formula is
Code:
=IF(WEEKDAY(AE2662)<=4,AE2662+4-WEEKDAY(AE2662),AE2662+11-WEEKDAY(AE2662))

and this is where I am with trying to imitate this in Access
Code:
NextWed: IIf(Weekday([due]<=4),[due]+4)-Weekday([due]),[due]+11-weekday[due]))

I'm getting the expression you entered contains invalid syntax or you need to enclose your text with quotes at the false part of the IIF statement but cannot figure out where I am going wrong.

Any help or suggestions would be much appreciated
 

DavidCon

Registered User.
Local time
Yesterday, 19:02
Joined
Apr 14, 2011
Messages
23
Just in case anyone needs this: this works:


NextWed: IIf(Weekday([due]<=4),([due]+4)-Weekday([due]),([due]+11-Weekday([due])))
 

isladogs

MVP / VIP
Local time
Today, 03:02
Joined
Jan 14, 2017
Messages
18,261
There is an error in your function - bracket wrongly placed
As a result, when run today (21/12/2017) your function gives 20/12/2017

It should read (note red bracket position)
Code:
IIf(Weekday([Due][COLOR="Red"])[/COLOR] <= 4, [Due] + 4 - Weekday([Due]), [Due] + 11 - Weekday([Due]))

This gives the correct answer - 27/12/2017

NOTE: I've also removed unnecessary extra brackets so its easier to read
 
Last edited:

Gasman

Enthusiastic Amateur
Local time
Today, 03:02
Joined
Sep 21, 2011
Messages
14,423
Just in case anyone needs this: this works:


NextWed: IIf(Weekday([due]<=4),([due]+4)-Weekday([due]),([due]+11-Weekday([due])))

I don't have my code to hand, as it is in work, but I did something like this, although I wanted Mondays so used vbMonday ?, however it appears just changing the startday of the week, gets you the next reuired day which happens to be the startday?

Code:
DateAdd("d", 8 - Weekday(due, vbWednesday), due)

HTH
 
Last edited:

Pat Hartman

Super Moderator
Staff member
Local time
Yesterday, 22:02
Joined
Feb 19, 2002
Messages
43,457
Here's an example with lots of useful date functions.
 

Attachments

  • UsefulDateFunctions171121.zip
    192.3 KB · Views: 64

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 10:02
Joined
May 7, 2009
Messages
19,246
you're right Gasman.
in fact it can be extended to
a generic function.
Returns the Next whatever
Weekday you want:
Code:
Public Function NextWeekDay(d As Date, intDayOfWeek As VbDayOfWeek) As Date
    NextWeekDay = DateAdd("d", 8 - Weekday(d, intDayOfWeek), d)
End Function
 

Users who are viewing this thread

Top Bottom