A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Access » Running & Setting Up Queries
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

Calculating the Number of Days between two dates



 
 
Thread Tools Display Modes
  #1  
Old July 20th, 2008, 07:57 PM posted to microsoft.public.access.queries
[email protected]
external usenet poster
 
Posts: 1
Default Calculating the Number of Days between two dates

I need to calculate the number of working days (MondayFriday) between two
dates, for example 10/07/08 - 18/07/08, would be a total of 7 working days.
On Excel this is simply done by using networkdays, but how do i go about
doing this on Access. Im not too fussed about bank holidays, but if there
are any geniuses out there that know how to do this as well that would be a
great help.
  #2  
Old July 20th, 2008, 08:55 PM posted to microsoft.public.access.queries
Damon Heron[_2_]
external usenet poster
 
Posts: 237
Default Calculating the Number of Days between two dates

This site has a function you can use:
http://www.mvps.org/access/datetime/date0012.htm

Damon

"
wrote in message ...
I need to calculate the number of working days (MondayFriday) between two
dates, for example 10/07/08 - 18/07/08, would be a total of 7 working
days.
On Excel this is simply done by using networkdays, but how do i go about
doing this on Access. Im not too fussed about bank holidays, but if there
are any geniuses out there that know how to do this as well that would be
a
great help.



  #4  
Old July 21st, 2008, 12:00 AM posted to microsoft.public.access.queries
Ken Sheridan
external usenet poster
 
Posts: 3,433
Default Calculating the Number of Days between two dates

The following function does this, and also caters, optionally, for public
holidays. Moreover it handles the different public holidays in the
constituent counties of the UK and of the Republic of Ireland by means of the
Country argument.

Public Function WorkDaysDiff(varLastDate As Variant, _
varFirstDate As Variant, _
strCountry As String, _
Optional blnExcludePubHols As Boolean = False)
As Variant

Dim lngDaysDiff As Long, lngWeekendDays As Long
Dim intPubHols As Integer

If IsNull(varLastDate) Or IsNull(varFirstDate) Then
Exit Function
End If

' if first date is Sat or Sun start on following Monday
Select Case WeekDay(varFirstDate, vbMonday)
Case vbSaturday
varFirstDate = varFirstDate + 2
Case vbSunday
varFirstDate = varFirstDate + 1
End Select

' if last date is Sat or Sun finish on following Monday
Select Case WeekDay(varLastDate, vbMonday)
Case vbSaturday
varLastDate = varLastDate + 2
Case vbSunday
varLastDate = varLastDate + 1
End Select

' get total date difference in days
lngDaysDiff = DateDiff("d", varFirstDate, varLastDate)

' get date difference in weeks and multiply by 2
' to get number of weekend days
lngWeekendDays = DateDiff("ww", varFirstDate, varLastDate, vbMonday) * 2

' subtract number of weekend days from total date difference
' to return number of working days
WorkDaysDiff = lngDaysDiff - lngWeekendDays

' exclude public holidays if required
If blnExcludePubHols Then
intPubHols = DCount("*", "qryPubHols", "HolDate Between #" _
& Format(varFirstDate, "mm/dd/yyyy") & "# And #" & _
Format(varLastDate - 1, "mm/dd/yyyy") & "#" & _
" And Country = """ & strCountry & """")

WorkDaysDiff = WorkDaysDiff - intPubHols
End If

End Function

The qryPubHols query referred to in the function is:

SELECT Countries.Country, PubHols.HolDate
FROM PubHols INNER JOIN (Countries INNER JOIN PubHols_Country
ON Countries.CountryID = PubHols_Country.CountryID)
ON PubHols.HolDateID = PubHols_Country.HolDateID;

As you see this is based on 3 tables: Countries with columns CountryID and
Country,
PubHols_Country with columns CountryID and HolDateID, and PubHols with
columns HolDateID and HolDate. It was first written many years ago; if I
were doing it now I'd get rid of the HolDateID and CountryID columns and use
Country and HolDate as the keys. The query would not then only need to
include the PubHols_Country table. In fact the table itself could be the
domain argument of the DCount function.

If you are only concerned with one set of UK public holidays you can remove
the Country argument from the function and just have a single-column PubHols
table. On the other hand, while it was originally designed for UK/Republic
of Ireland use, it can be extended internationally simply by inserting more
rows into the three tables with additional countries and their public holiday
dates.

Ken Sheridan
Stafford, England

" wrote:

I need to calculate the number of working days (MondayFriday) between two
dates, for example 10/07/08 - 18/07/08, would be a total of 7 working days.
On Excel this is simply done by using networkdays, but how do i go about
doing this on Access. Im not too fussed about bank holidays, but if there
are any geniuses out there that know how to do this as well that would be a
great help.


  #5  
Old July 21st, 2008, 12:24 AM posted to microsoft.public.access.queries
Ken Sheridan
external usenet poster
 
Posts: 3,433
Default Calculating the Number of Days between two dates

Correction: Function should have been:

Public Function WorkDaysDiff(varLastDate As Variant, _
varFirstDate As Variant, _
strCountry As String, _
Optional blnExcludePubHols As Boolean = False)
As Variant

Dim lngDaysDiff As Long, lngWeekendDays As Long
Dim intPubHols As Integer

If IsNull(varLastDate) Or IsNull(varFirstDate) Then
Exit Function
End If

' if first date is Sat or Sun start on following Monday
Select Case WeekDay(varFirstDate, vbSunday)
Case vbSaturday
varFirstDate = varFirstDate + 2
Case vbSunday
varFirstDate = varFirstDate + 1
End Select

' if last date is Sat or Sun finish on following Monday
Select Case WeekDay(varLastDate, vbSunday)
Case vbSaturday
varLastDate = varLastDate + 2
Case vbSunday
varLastDate = varLastDate + 1
End Select

' get total date difference in days
lngDaysDiff = DateDiff("d", varFirstDate, varLastDate)

' get date difference in weeks and multiply by 2
' to get number of weekend days
lngWeekendDays = DateDiff("ww", varFirstDate, varLastDate, vbMonday) * 2

' subtract number of weekend days from total date difference
' to return number of working days
WorkDaysDiff = lngDaysDiff - lngWeekendDays

' exclude public holidays if required
If blnExcludePubHols Then
intPubHols = DCount("*", "qryPubHols", "HolDate Between #" _
& Format(varFirstDate, "mm/dd/yyyy") & "# And #" & _
Format(varLastDate - 1, "mm/dd/yyyy") & "#" & _
" And Country = """ & strCountry & """")

WorkDaysDiff = WorkDaysDiff - intPubHols
End If

End Function

One other thing I should have mentioned. The function works like the built
in DateDiff function (hence its name) and counts exclusive of the last date,
so 10/07/08 - 18/07/08 would be 6 working days, not 7. To make it inclusive
amend it like so:

If IsNull(varLastDate) Or IsNull(varFirstDate) Then
Exit Function
Else
varLastDate = DateAdd("d", 1, varLastDate)
End If

Ken Sheridan
Stafford, England

 




Thread Tools
Display Modes

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

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 05:31 AM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.