Microsoft KB Archive/97757

= ACC: How to Determine Number of Working Days Between Two Dates =

Article ID: 97757

Article Last Modified on 1/18/2007

-

APPLIES TO


 * Microsoft Access 1.0 Standard Edition
 * Microsoft Access 1.1 Standard Edition
 * Microsoft Access 2.0 Standard Edition
 * Microsoft Access 95 Standard Edition
 * Microsoft Access 97 Standard Edition

-



This article was previously published under Q97757



Moderate: Requires basic macro, coding, and interoperability skills.



SUMMARY
This article shows you how to calculate the number of working days between two dates.

This article assumes that you are familiar with Visual Basic for Applications and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information about Visual Basic for Applications, please refer to your version of the &quot;Building Applications with Microsoft Access&quot; manual.

NOTE: Visual Basic for Applications is called Access Basic in Microsoft Access versions 1.x and 2.0. For more information about Access Basic, please refer to the &quot;Introduction to Programming&quot; manual in Microsoft Access version 1.x or the &quot;Building Applications&quot; manual in Microsoft Access version 2.0



MORE INFORMATION
Microsoft Access does not have a function to determine the number of working days between two given dates. To determine this, you need to call a user-defined function. The following function includes the start date and the end date; so, the number of days between 02/02/93 and 02/03/93 equals one.

To create the function, follow these steps.

NOTE: In the following sample code, an underscore (_) is used as a line- continuation character. Remove the underscore from the end of the line when re-creating this code in Access Basic. Option Explicit

Function Work_Days (BegDate As Variant, EndDate As Variant) As Integer ' Note that this function does not account for holidays. Dim WholeWeeks As Variant Dim DateCnt As Variant Dim EndDays As Integer

BegDate = DateValue(BegDate) EndDate = DateValue(EndDate) WholeWeeks = DateDiff(&quot;w&quot;, BegDate, EndDate) DateCnt = DateAdd(&quot;ww&quot;, WholeWeeks, BegDate) EndDays = 0 Do While DateCnt < EndDate If Format(DateCnt, &quot;ddd&quot;) <> &quot;Sun&quot; And _ Format(DateCnt, &quot;ddd&quot;) <> &quot;Sat&quot; Then EndDays = EndDays + 1 End If        DateCnt = DateAdd(&quot;d&quot;, 1, DateCnt) Loop Work_Days = WholeWeeks * 5 + EndDays End Function To call the function, you can pass either a valid string or an actual date value. The following are two ways to call this function from the Debug window (or Immediate window in Microsoft Access 2.0 or earlier): ?Work_Days(&quot;01/01/93&quot;, &quot;12/31/96&quot;) (returns 1042)

?Work_Days(#03/05/93#, #04/06/93#) (returns 22

Additional query words: modules day/time workday workdays weekday weekdays

Keywords: kbhowto kbprogramming KB97757

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.