Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access
Access Discussion of Microsoft Access database design and programming. See also the forums for Access ASP and Access VBA.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old March 30th, 2008, 03:40 PM
Authorized User
Join Date: Apr 2005
Posts: 56
Thanks: 0
Thanked 0 Times in 0 Posts
Default DateDiff to exclude weekends and holidays


I am working on an On Time Delivery Report that is generated on a monthly basis. The form specifies a single customer name and the date fields are From: and To: Ie. From 12/01/07 to 12/31/07. I need to count workdays only. Weekends and Holidays need to be excluded. There can be as many as 100 orders for a specific customer any given month. Each order appears on a separate line on the report.

I placed the following code in the On Format field of the report

Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)

Dim WorkDays As String

WorkDays = DateDiff("d", Me.JobEntryDateText, Me.DDate)

Me.Days.Value = WorkDays

End Sub

"d" gives me the total days (counting the weekends and holidays) example - 12/21/06 to 01/03/07 = 13 days

"w" gives me the number of full 7 day weeks - example 12/21/06 to 01/03/07 = 1

Any help will be appreciated! Thanks

D. Bartelt
D. Bartelt
Old April 8th, 2008, 12:22 PM
Authorized User
Join Date: Apr 2005
Posts: 56
Thanks: 0
Thanked 0 Times in 0 Posts

Okay...Do I need to be using a different function than the DateDiff function?

Any suggestions?


D. Bartelt
Old April 12th, 2008, 05:14 PM
Authorized User
Join Date: Jan 2006
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts

Here's a function you can put in a standard module.
Public Function WorkingDays(StartDate As Date, EndDate As Date) As Integer
'-- Return the number of WorkingDays between StartDate and EndDate
On Error GoTo err_workingDays

Dim intCount As Integer

If IsDate(StartDate) And IsDate(EndDate) Then
   If EndDate >= StartDate Then

      intCount = 0
      Do While StartDate < EndDate
         StartDate = StartDate + 1
         If Weekday(StartDate, vbMonday) <= 5 Then
      '-- Use the following code if you have a "Holiday" table
'         If Weekday(StartDate, vbMonday) <= 5 And _
            IsNull(DLookup("[Holiday]", "tblHolidays", _
            "[HolDate] = " & Format(StartDate, "\#mm\/dd\/yyyy\#;;;\N\u\l\l"))) Then

            intCount = intCount + 1
         End If
      WorkingDays = intCount
      WorkingDays = -1  '-- To show an error
   End If
   WorkingDays = -1  '-- To show an error
End If

   Exit Function

   MsgBox "Error No:    " & Err.Number & vbCr & _
   "Description: " & Err.Description
   Resume exit_workingDays

End Function
HTH RuralGuy (RG for short) acXP WinXP Pro

Similar Threads
Thread Thread Starter Forum Replies Last Post
Date diff excluding holidays and weekends Altonjain Classic ASP Professional 0 February 9th, 2006 12:56 AM
DateDiff?? ( w/o weekends... ) underscore10304 Access VBA 7 January 24th, 2006 08:55 AM
Exclude Nodes seanhaggerty XSLT 1 February 3rd, 2005 06:00 AM
Exclude Certain Forms Ben Horne Access VBA 11 March 16th, 2004 03:06 PM
Calculate Workdays minus holidays woodan Access 1 February 11th, 2004 04:17 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.