Solved

Access 2010 VBA Function Calculate Workdays

Posted on 2014-12-02
6
1,005 Views
Last Modified: 2014-12-02
I'm using the code below to calculate workdays and I'm try to figure out how to incorporate holidays in the function. Any suggestions??

 Dim WholeWeeks As Variant
      Dim DateCnt As Variant
      Dim EndDays As Integer

      BegDate = DateValue(BegDate)
      EndDate = DateValue(EndDate)
      WholeWeeks = DateDiff("w", BegDate, EndDate)
      DateCnt = DateAdd("ww", WholeWeeks, BegDate)
      EndDays = 0
      Do While DateCnt < EndDate
         If Format(DateCnt, "ddd") <> "Sun" And _
                          Format(DateCnt, "ddd") <> "Sat" Then
            EndDays = EndDays + 1
         End If
         DateCnt = DateAdd("d", 1, DateCnt)
      Loop
      Work_Days = WholeWeeks * 5 + EndDays
0
Comment
Question by:shieldsco
  • 3
  • 3
6 Comments
 
LVL 57
ID: 40476783
You need to add a table of holidays, then check the dates as you add if it's a holiday or not.  Only way to do it.

You can optimize a bit; get a recordset of holidays between start and end, sorted in ascending sequence.  Then check on each iteration of the loop, and if = skip and fetch the next holiday record.

Jim.
0
 
LVL 57
ID: 40476791
the other way to do it of course is to simply count the number of holidays between start and end and subtract at the end.

Jim.
0
 

Author Comment

by:shieldsco
ID: 40476809
what would the code look like incorporated into the aforementioned code?
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 57

Accepted Solution

by:
Jim Dettman (Microsoft MVP/ EE MVE) earned 500 total points
ID: 40476843
Work_Days = WholeWeeks * 5 + EndDays  - Dcount("*","tblHolidays","[HolidayDate] Between #" & Format$(BegDate,"MM/DD/YY")  & "#  AND #" & Format$(EndDate,"MM/DD/YY") & "#")

 assuming tblHolidays only contains dates that are not Saturday or Sunday.

Jim.
0
 

Author Comment

by:shieldsco
ID: 40476896
Jim let me test
0
 

Author Closing Comment

by:shieldsco
ID: 40476993
Good Job Jim....Thanks
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

828 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