I am working in a query and have struggled to solve this problem.
I have a date "ClockStart " and I want to add 2, 3 and 6 months to this date in separate fields but the result must be on a work day NOT a weekend. For the straight addition of months I know I can use DateAdd:
I have a module code that I have used when adding DAYS to a date which results on a work day but don't know how to amend (if possible) for MONTHS:
Expression used in query:
An example of dates that fall on weekends are:
ClockStart 03/12/2012 (Monday)
2 months: 03/02/2012 (Sunday)
3 months: 03/02/2012 (Sunday)
Hope you can help.
I have a date "ClockStart " and I want to add 2, 3 and 6 months to this date in separate fields but the result must be on a work day NOT a weekend. For the straight addition of months I know I can use DateAdd:
Code:
DateAdd("m",2,[ClockStart])
Code:
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
Code:
funReturnWkdy([ClockStart],4)
ClockStart 03/12/2012 (Monday)
2 months: 03/02/2012 (Sunday)
3 months: 03/02/2012 (Sunday)
Hope you can help.
Comment