Solved

DateAdd for business days or calendar days which ever is greater

Posted on 2008-10-13
4
690 Views
Last Modified: 2013-11-28
capricorn1 was able to help with the below answer.  I have another question that is a bit more complex.  My original question was how to create a module and expression to calculate business days.  

The answer is:

"place this codes in  a module

Function getDeadLineDate(dDate As Date, Span As Integer) As Date
Dim j As Integer, i As Integer, dtStart
dtStart = dDate
For j = 1 To Span
    dtStart = dtStart + 1
    Do While Weekday(dtStart) = 1 Or Weekday(dtStart) = 7 '_
        dtStart = dtStart + 1
        i = i + 1
    Loop
Next
getDeadLineDate = DateAdd("d", i + Span, dDate)

End Function

to use

If [State or Federal Mandates_ID] = 4 And [Review Level_ID] = 2 Then
    [Determination Compliance Deadline D/T] = getDeadLineDate([Request D/T],2)
End If"

I have another question...  I want to use a similar expression to decide to use "which ever is greater" when it comes to business days or calendar days.  I need a formula that calculates either 1 business day or 72 hours which ever is greater.  This seems to only be a problem on Fridays.  I need it to use 1 business day if it is Friday otherwise use 72 hours.

Thanks for the help!
0
Comment
Question by:dddw
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 22708269
define how 1 business day can be greater then 72 hours and vice versa.
0
 
LVL 50

Accepted Solution

by:
Gustav Brock earned 500 total points
ID: 22709150
> I need it to use 1 business day if it is Friday otherwise use 72 hours.

But if it is Thursday that would return Sunday. Probably not what you want.

Your next question will probably be how to skip a juxtaposed holiday. So, what you can do is to use the functions below like this:

datToday = Date
datNext = DateSkipNonWorkingday(DateAdd("d", 3, datToday))

This way datToday can be any weekday - including weekend - while datNext will be at least three days later skipping any weekend day or holiday.

/gustav
Public Function DateSkipNoneWorkingday( _
  ByVal datDate As Date, _
  Optional ByVal booReverse As Boolean) _
  As Date
 
' Purpose: Calculate first working day following/preceding datDate.
'
' May be freely used and distributed.
' 1999-07-03, Cactus Data ApS, CPH
  
  Dim datNext As Date
  Dim datTest As Date
    
  datNext = datDate
  Do
    datTest = datNext
    datNext = DateSkipHoliday(datTest, booReverse)
    datNext = DateSkipWeekend(datNext, booReverse)
  Loop Until DateDiff("d", datTest, datNext) = 0
  
  DateSkipNoneWorkingday = datNext
 
End Function
 
Public Function DateSkipWeekend( _
  ByVal datDate As Date, _
  Optional ByVal booReverse As Boolean) _
  As Date
 
' Purpose: Calculate first working day equal to or following/preceding datDate.
' Assumes: 5 or 6 working days per week. Weekend is (Saturday and) Sunday.
' Limitation: Does not count for public holidays.
'
' May be freely used and distributed.
' 1999-07-03, Cactus Data ApS, CPH
  
  Const cintWorkdaysOfWeek As Integer = 5
 
  Dim bytSunday   As Byte
  Dim bytWeekday  As Byte
  
  bytSunday = WeekDay(vbSunday, vbMonday)
  bytWeekday = WeekDay(datDate, vbMonday)
  
  If bytWeekday > cintWorkdaysOfWeek Then
    ' Weekend.
    If booReverse = False Then
      ' Get following workday.
      datDate = DateAdd("d", 1 + bytSunday - bytWeekday, datDate)
    Else
      ' Get preceding workday.
      datDate = DateAdd("d", cintWorkdaysOfWeek - bytWeekday, datDate)
    End If
  End If
 
  DateSkipWeekend = datDate
 
End Function
 
Public Function DateSkipHoliday( _
  ByVal datDate As Date, _
  Optional ByVal booReverse As Boolean) _
  As Date
 
' Purpose: Calculate first day following/preceding datDate if this is holiday.
'
' May be freely used and distributed.
' 1999-07-03, Cactus Data ApS, CPH
  
  ' Adjust to fit your table of holidays.
  Const cstrHolidayTable  As String = "tblHoliday"
  Const cstrHolidayField  As String = "HolidayDate"
  
  While Not IsNull(DLookup(cstrHolidayField, cstrHolidayTable, cstrHolidayField & " = " & Format(datDate, "\#m\/d\/yyyy\#")))
    datDate = DateAdd("d", 1 - Abs(2 * booReverse), datDate)
  Wend
 
  DateSkipHoliday = datDate
 
End Function

Open in new window

0
 

Author Closing Comment

by:dddw
ID: 31505741
Thank you!
0
 
LVL 50

Expert Comment

by:Gustav Brock
ID: 22711551
You are welcome!

/gustav
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

690 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question