Connecting Tech Pros Worldwide Forums | Help | Site Map

DateAdd exclude Weekends

Newbie
 
Join Date: Sep 2008
Posts: 10
#1: 4 Weeks Ago
I know this is a 'dead horse' here however, I've searched this site and still can't get it right...

I have a field named [court_date] and a field named [serve_by]. The [served_by] date has to be 7 working days before the [court date].

I copied and pasted the following function from this site:
Expand|Select|Wrap|Line Numbers
  1. '********************************************************** 
  2. 'Declarations section of the module 
  3. '********************************************************** 
  4.  
  5. Option Explicit 
  6.  
  7. '========================================================== 
  8. ' The DateAddW() function provides a workday substitute 
  9. ' for DateAdd("w", number, date). This function performs 
  10. ' error checking and ignores fractional Interval values. 
  11. '========================================================== 
  12. Function DateAddW (ByVal TheDate, ByVal Interval) 
  13.  
  14.    Dim Weeks As Long, OddDays As Long, Temp As String 
  15.  
  16.    If VarType(TheDate) <> 7 Or VarType(Interval) < 2 Or _ 
  17.               VarType(Interval)  > 5 Then 
  18.       DateAddW = TheDate 
  19.    ElseIf Interval = 0 Then 
  20.       DateAddW = TheDate 
  21.    ElseIf Interval > 0 Then 
  22.       Interval = Int(Interval) 
  23.  
  24.    ' Make sure TheDate is a workday (round down). 
  25.  
  26.       Temp = Format(TheDate, "ddd") 
  27.       If Temp = "Sun" Then 
  28.          TheDate = TheDate - 2 
  29.       ElseIf Temp = "Sat" Then 
  30.          TheDate = TheDate - 1 
  31.       End If 
  32.  
  33.    ' Calculate Weeks and OddDays. 
  34.  
  35.       Weeks = Int(Interval / 5) 
  36.       OddDays = Interval - (Weeks * 5) 
  37.       TheDate = TheDate + (Weeks * 7) 
  38.  
  39.   ' Take OddDays weekend into account. 
  40.  
  41.       If (DatePart("w", TheDate) + OddDays) > 6 Then 
  42.          TheDate = TheDate + OddDays + 2 
  43.       Else 
  44.          TheDate = TheDate + OddDays 
  45.       End If 
  46.  
  47.       DateAddW = TheDate 
  48. Else                         ' Interval is < 0 
  49.       Interval = Int(-Interval) ' Make positive & subtract later. 
  50.  
  51.    ' Make sure TheDate is a workday (round up). 
  52.  
  53.       Temp = Format(TheDate, "ddd") 
  54.       If Temp = "Sun" Then 
  55.          TheDate = TheDate + 1 
  56.       ElseIf Temp = "Sat" Then 
  57.          TheDate = TheDate + 2 
  58.       End If 
  59.  
  60.    ' Calculate Weeks and OddDays. 
  61.  
  62.       Weeks = Int(Interval / 5) 
  63.       OddDays = Interval - (Weeks * 5) 
  64.       TheDate = TheDate - (Weeks * 7) 
  65.  
  66.    ' Take OddDays weekend into account. 
  67.  
  68.       If (DatePart("w", TheDate) - OddDays) > 2 Then 
  69.          TheDate = TheDate - OddDays - 2 
  70.       Else 
  71.          TheDate = TheDate - OddDays 
  72.       End If 
  73.  
  74.       DateAddW = TheDate 
  75.     End If 
  76.  
  77. End Function
However, I can't get it to work.

When I put in the code:
Expand|Select|Wrap|Line Numbers
  1. Serve_by= DateAddW ("d", -7, [court_date])
I get "Complie Error: Wrong Number of Arguments or Invalid Property Assignments"

When I put in the code:
Expand|Select|Wrap|Line Numbers
  1. Serve_by= DateAddW(-7, [Court_date])
It returns "01/01/2010"

What am I doing Wrong?

nico5038's Avatar
Moderator
 
Join Date: Nov 2006
Location: The Netherlands
Posts: 2,232
#2: 4 Weeks Ago

re: DateAdd exclude Weekends


You should use:
DateAddW( [Court_date], -7)
as the function has two arguments:
TheDate AND Interval

Nic;o)
Newbie
 
Join Date: Sep 2008
Posts: 10
#3: 4 Weeks Ago

re: DateAdd exclude Weekends


Thank you for your reply,

However, It seems that the function is counting 7 weeks, not 7 days. Is there something that I need to change in the function or the code?
NeoPa's Avatar
Administrator
 
Join Date: Oct 2006
Location: London - UK
Posts: 15,730
#4: 4 Weeks Ago

re: DateAdd exclude Weekends


It's a function to move a number of weeks.

Check out Function to Move (Forwards or Backwards) Through Weekdays instead.
Newbie
 
Join Date: Sep 2008
Posts: 10
#5: 4 Weeks Ago

re: DateAdd exclude Weekends


How do I call the function?
NeoPa's Avatar
Administrator
 
Join Date: Oct 2006
Location: London - UK
Posts: 15,730
#6: 4 Weeks Ago

re: DateAdd exclude Weekends


Expand|Select|Wrap|Line Numbers
  1. RequiredDate = MoveWD(datThis:=OriginalDate, intInc:=NoOfWDays)
Reply


Similar Microsoft Access / VBA bytes