totn Access

MS Access 2003: Calculate the number of workdays between two dates

This MSAccess tutorial explains how to calculate the number of workdays betwen two dates in Access 2003 (with screenshots and step-by-step instructions).

Question: In Microsoft Access 2003/XP/2000/97, I need to calculate the number of workdays between two dates (ie: elapsed days excluding Saturdays and Sundays).

For example, if I have 3/4/2005 and 3/29/2005, the number of workdays is 17. How can I calculate this?

Answer: You can calculate the number of workdays between two dates using our function below:

Open your Access database, click on the Modules tab and create a new Module. Paste in the following code:

Function CalcWorkdays(StartDate, EndDate) As Integer

   Dim LTotalDays As Integer
   Dim LSaturdays As Integer
   Dim LSundays As Integer

   On Error GoTo Err_Execute

   CalcWorkdays = 0

   If IsDate(StartDate) And IsDate(EndDate) Then
      If EndDate <= StartDate Then
         CalcWorkdays = 0
      Else
         LTotalDays = DateDiff("d", StartDate - 1, EndDate)
         LSaturdays = DateDiff("ww", StartDate - 1, EndDate, 7)
         LSundays = DateDiff("ww", StartDate - 1, EndDate, 1)

         'Workdays is the elapsed days excluding Saturdays and Sundays
         CalcWorkdays = LTotalDays - LSaturdays - LSundays

      End If

   End If

   Exit Function

Err_Execute:
   'If error occurs, return 0
   CalcWorkdays = 0

End Function

Here is an example of how you might call this function in a query:

Microsoft Access