PC Review


Reply
Thread Tools Rate Thread

DateDiff Working Days only not including holidays code problem

 
 
Kevin
Guest
Posts: n/a
 
      12th Oct 2008
Hi,

I have found this code to work out date difference (working days only) and
to include holidays from my [tblHoliDate] table the code seems to work if
only one date in my holiday table falls between the [StartDate] and
[Enddate], but if there is more than one holiday date between [StartDate] and
[EndDate] it only seems to take off the first. (I hope this makes sense!).

Could any tell me where I am going wrong Please?



Public Function CountWorkingDays(StartDate As Date, EndDate As Date) As
Integer

On Error GoTo ErrorHandler
'Get the number of workdays between the given dates
'function uses the Holidays table and deducts them from the days to allow
'for weekends
'and holidays when calculating deadline dates

Dim dbs As Database
Dim rstHolidays As Recordset

Dim lngIndex As Long
'Dim MyDate As Date
Dim lngNumDays As Long
Dim strCriteria As String
Dim NumSgn As String * 1

Set dbs = CurrentDb
Set rstHolidays = dbs.OpenRecordset("Holidays", dbOpenDynaset)

NumSgn = Chr(35)

StartDate = Format(StartDate, "Short Date")

For lngIndex = CLng(StartDate) To CLng(EndDate)
Select Case (Weekday(StartDate))
Case Is = 1
'Do Nothing, it is Sunday
Case Is = 7
'Do Nothing, it is Saturday
Case Else 'Normal Workday
strCriteria = "[HoliDate] = " & NumSgn & StartDate & NumSgn
rstHolidays.FindFirst strCriteria
If (rstHolidays.NoMatch) Then
lngNumDays = lngNumDays + 1
Else
'Do Nothing, it is NOT a Workday
End If
End Select
StartDate = DateAdd("d", 1, StartDate)
Next lngIndex

CountWorkingDays = lngNumDays
Exit Function
ErrorHandler:
'on error destroy objects and exit function... The function will return
a zero value
Set rstHolidays = Nothing
Set dbs = Nothing
Exit Function
End Function




 
Reply With Quote
 
 
 
 
Kevin
Guest
Posts: n/a
 
      13th Oct 2008

Thank You works a treat
"ruralguy via AccessMonster.com" wrote:

> Hi Bob,
> The backslashes "\" put the next character in the string absolutely. Since
> the output of the Format() function is always a string, it returns Null when
> the value of StartDate is null.
>
> raskew wrote:
> >ruralguy,
> >
> >Would you explain the formatting portion of:
> >
> >If Weekday(StartDate, vbMonday) <= 5 And _
> > IsNull(DLookup("[Holiday]", "tblHolidays", _
> > "[HolDate] = " & Format(StartDate, "\#mm\/dd\/yyyy\#;;;\N\u\l\l"))
> >
> >Why the back-slashes and what's #;;;\N\u\l\l"))?
> >
> >Thanks and best wishes,
> >
> >Bob
> >
> >>Kevin,
> >>Here's the function I use. Maybe that will help.

> >[quoted text clipped - 44 lines]
> >>> Exit Function
> >>>End Function

>
> --
> RuralGuy (RG for short) aka Allan Bunch MS Access MVP - acXP WinXP Pro
> Please post back to this forum so all may benefit.
>
> Message posted via http://www.accessmonster.com
>
>

 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with Code and Working Days including Holidays Kevin Microsoft Access 14 13th Oct 2008 07:31 PM
DateDiff Working Days only not including holidays code problem Kevin Microsoft Access VBA Modules 1 12th Oct 2008 08:46 PM
formula for total days, excluding holidays but including weekends wtfisch Microsoft Excel Discussion 3 8th May 2008 06:26 PM
Business Days Including Holidays =?Utf-8?B?RG1vcnJpMjU0?= Microsoft Excel Programming 10 4th May 2006 12:49 AM
to add a # of days... not including holidays/weekends Aaron Microsoft Access Queries 2 23rd Feb 2004 09:45 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:50 AM.