|
Question : dateadd function returns weekends even when weekday "w" is specified
|
|
dateadd function returns weekends even when weekday "w" is specified . Example, day_1: DateAdd("w",-1,Histmf_8002_02_1!date) returns Sunday 10/21/2001 when given Monday 10/22/2001 as date. day_1: DateAdd("w",1,Histmf_8002_02_1!date) returns Saturday 10/20/2001 when given 10/19/2001 as date.
Is there a fix or workaround?
|
|
Answer : dateadd function returns weekends even when weekday "w" is specified
|
|
Create the following function in a module. Call the function passing the date you want to evaluate plus a positive or negative number you want to add or subtract to the date passed. If you pass a negative number and it lands on a Sat/Sun, it assumes you want to go back to the prior Friday. If you pass a positive number, it assumes you want to go forward to the next Monday.
-Rachel Morris M.G.C.D. Consulting [email protected]
Public Function funReturnWkdy(dtStartDte As Date, sngNumDysToAdd As Single) As Date ' Note - sngNumDysToAdd can be positive or negative ' Function assumes that if you are passing a reduction, aka a negative ' sngNumDysToAdd, you want to return to the prior Friday if ' date passed equals Saturday or Sunday, and if you pass a positive ' sngNumDysToAdd, you want to move forward to the next Monday
Dim dtTest As Date, sngAdjustDy As Single If sngNumDysToAdd < 0 Then sngAdjustDy = -1 Else sngAdjustDy = 1
dtTest = DateAdd("d", sngNumDysToAdd, dtStartDte)
Select Case Weekday(dtTest) Case 1 ' Sunday If sngAdjustDy < 0 Then dtTest = DateAdd("d", sngAdjustDy + sngAdjustDy, dtTest) Else dtTest = DateAdd("d", sngAdjustDy, dtTest) End If Case 7 If sngAdjustDy < 0 Then dtTest = DateAdd("d", sngAdjustDy, dtTest) Else dtTest = DateAdd("d", sngAdjustDy + sngAdjustDy, dtTest) End If End Select funReturnWkdy = dtTest End Function
|
|
|
|