Business Days Calculation

M

Max

Hello, I am trying to calculate Business Days between
dates excluding Holidays and weekends. I copied the
recommended function including tables and modules into my
database (from Utter Access) and put the following code
into the control source....I want to calculate the age
between [From A/E] and [To A/E]. These are date fields.
When [From A/E] is null I want to use current date - [To
A/E] excluding weekends and holidays.

=IIf(IsNull([From A/E]),(DiffBusinessDays_RRR(Date(),[To
A/E])*-1),DiffBusinessDays_RRR([From A/E],[To A/E]))

I get a VB error "There was an error compiling this
function. The Visual Basic module contains a syntax
error. Check the code, and then recompile it."

I originally used the Work_Days function from
http://www.mvps.org/access/datetime/date0006.htm, but the
days weren't correct.

Here is the function:
Function DiffBusinessDays_RRR(datDay1 As Date, _
datDay2 As Date, _
Optional Exclude_Holidays As
Boolean = True, _
Optional Excluded_Days As
String = "Sat,Sun", _
Optional ReqdCountry As
String = glb_ReqdCountry, _
Optional strHolidayTbl As
String = "tbl_HolidayDates", _
Optional strHolidayDate As
String = "HolidayDate") As Long

' Comments : Returns the number of business days between
two dates
' The days are rounded down -- it takes 24
hours to make a day.
' Days defined as "Excluded Days" (normally
Saturday and Sunday)
' and holidays are not
counted (if requested).
'
' ** If there are to be NO Excluded Days
(i.e. Sat, Sun to be included)
' then this parameter should be set as
NULL or as ""


' Parameters: datDay1 - first (earlier) date/time
(subtracted from datDay2)
' datDay2 - second (later) date/time
' strHolidayTbl - name of holiday table
' strHolidayDate - field name of holiday
dates in the holiday table
' Returns : Number of whole business days between two
dates
' (Returns negative days if datDay1 is after
datDay2)

Dim db As Database
Dim rst As Recordset

Dim strSQL As String, strField As String
Dim lngBusinessDays As Long, lngWeekdays As Long

If datDay1 <= datDay2 Then
lngWeekdays = DiffWeekDays_RRR(datDay1, datDay2,
Excluded_Days)
Else
lngWeekdays = DiffWeekDays_RRR(datDay2, datDay1,
Excluded_Days)
End If

lngBusinessDays = 0

If Exclude_Holidays = True Then
If IsTableInDatabase(strHolidayTbl) = True Then

Set db = DBEngine(0)(0)

strField = "[" & strHolidayTbl & "].[" &
strHolidayDate & "]"

strSQL = "SELECT DISTINCTROW Count(" &
strField & ") AS Count" & _
" FROM [" & strHolidayTbl & "]" & _
"WHERE (((tbl_HolidayDates.Country)
= ""<All>""" & _
"OR (tbl_HolidayDates.Country) = " &
_
Chr$(39) & ReqdCountry & Chr$(39) & _
") AND ((" & strField

If datDay1 <= datDay2 Then
strSQL = strSQL & ">=#" & Format
(datDay1, "mm/dd/yyyy") & "# And " & _
strField & "<=#" &
Format(datDay2, "mm/dd/yyyy") & "#)))"
Else
strSQL = strSQL & ">=#" & Format
(datDay2, "mm/dd/yyyy") & "# And " & _
strField & "<=#" &
Format(datDay1, "mm/dd/yyyy") & "#)))"
End If

' MsgBox strSQL

Set rst = db.OpenRecordset(strSQL)
lngBusinessDays = rst![Count]
rst.Close
db.Close
Set rst = Nothing
Set db = Nothing
End If
End If

' MsgBox "Number of weekdays = " & lngWeekdays
' MsgBox "Number of holidays = " & lngBusinessDays
' MsgBox "Number of business days = " & (lngWeekdays -
lngBusinessDays)

DiffBusinessDays_RRR = lngWeekdays - lngBusinessDays

End Function

Any ideas....?????? I am borderline, so any assistance
would be a life saver.
Max
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top