Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find the working days between Dates

How to

Find the working days between Dates

by  Rolliee  Posted    (Edited  )
The following code in a global module will find the number of working days between two dates. Only Mondays thry Fridays are counted. If there is a table named "Holidays" the function opens it once and makes a public array of holidays. The first field must hold the dates. If no table is provided, only Christmas, The Fourth, New Years and MY Birthday are listed <G>

The form is

me.txtNumDays = GetDay( me.date1, me.date2)

Rollie E


Public HDi(12) As Date, iHDi As Integer

Public Function GetDay(ByVal d1 As Date, ByVal d2 As Date) As Long
Dim rs As DAO.Recordset, Numof As Long, i As Integer
Numof = 0

Do While d2 > d1
If good1(d2) Then Numof = Numof + 1
d2 = d2 - 1
Loop
GetDay = Numof
If iHDi = 0 Then
On Error GoTo tellme:
Set rs = CurrentDb.OpenRecordset("holidays")
rs.MoveLast: rs.MoveFirst
Do While Not rs.EOF
HDi(i) = rs.Fields(0)
iHDi = iHDi + 1
rs.MoveNext
Loop
GoTo around:
tellme:
If Err.Number = 3078 Then
HDi(0) = CDate("12/25/2004")
HDi(1) = CDate("7/4/2004")
HDi(2) = CDate("1/1/2005")
HDi(3) = CDATE("9/1/2004")
iHDi = 4
Exit Function
End If
around:
rs.Close
Set rs = Nothing
End If


End Function

Public Function good1(d2 As Date) As Boolean
Dim okay As Boolean, i As Integer

okay = False

Select Case Weekday(d2)
Case 2, 3, 4, 5, 6
okay = True

Case 1, 7
okay = False

End Select
good1 = okay
If okay Then
For i = 0 To iHDi - 1
If HDi(i) = d2 Then okay = False
Next
End If

End Function

Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top