All,
Please can anyone let me know why this is not working? I have the following code as a module in my access database:
Public Function NetWorkhours(dteStart As Date, dteEnd As Date) As Single
Dim intGrossDays As Integer
Dim intGrossHours As Single
Dim dteCurrDate As Date
Dim i As Integer
Dim WorkDayStart As Date
Dim WorkDayend As Date
Dim nonWorkDays As Integer
Dim StartDayhours As Single
Dim EndDayhours As Single
NetWorkhours = 0
nonWorkDays = 0
'Calculate work day hours on 1st and last day
WorkDayStart = DateValue(dteEnd) + TimeValue("08:00am")
WorkDayend = DateValue(dteStart) + TimeValue("05:00pm")
StartDayhours = DateDiff("n", dteStart, WorkDayend)
EndDayhours = DateDiff("n", WorkDayStart, dteEnd)
'adjust for time entries outside of business hours
If StartDayhours < 0 Then
StartDayhours = 0
End If
If EndDayhours > 9 Then
EndDayhours = 9
End If
'Calculate total hours and days between start and end times
intGrossDays = DateDiff("d", (dteStart), (dteEnd))
intGrossHours = DateDiff("n", (dteStart), (dteEnd))
'count number of weekend days and holidays (from a table called "Holidays" that lists them)
For i = 0 To intGrossDays
dteCurrDate = dteStart + i
If Weekday(dteCurrDate, vbSaturday) < 3 Then nonWorkDays = nonWorkDays + 1
If Not IsNull(DLookup("[HolDate]", "tblHolidays", "[HolDate] = #" & Int(dteCurrDate) & "#")) Then nonWorkDays = nonWorkDays + 1
Next i
'Calculate number of work hours
Select Case intGrossDays
Case 0
'start and end time on same day
NetWorkhours = intGrossHours
Case 1
'start and end time on consecutive days
NetWorkhours = NetWorkhours + StartDayhours
NetWorkhours = NetWorkhours + EndDayhours
Case Is > 1
'start and end time on non consecutive days
NetWorkhours = NetWorkhours - (nonWorkDays * 1)
NetWorkhours = (intGrossDays - 1 - nonWorkDays) * 9
NetWorkhours = NetWorkhours + StartDayhours
NetWorkhours = NetWorkhours + EndDayhours
End Select
End Function
The actual function works fine, ALTHOUGH I have set-up an "Outage Calculator" for the line manager to use. It is really simple. Contains three text boxes that are not bound to a table.
Box 1 --- Date\Time_From
Box 2 --- Date\Time_To
Box 3 --- Networkhours([Date\Time_From],[Date\Time_To])
Default value for box 1 and box 2 is Now() so the Line manager just adjusts the time and dates to suit his needs eg.
Date From Date To Network Minutes
20/11/2009 08:50:22 20/11/2009 10:50:22 120
The problem that I have is it seems to be ignoring weekends for some reason. If I put in:
Date From Date To Network Minutes
14/11/2009 08:50:22 15/11/2009 08:50:22 499
The 14th and 15th are Saturday and Sunday. It should only raise minutes for weekdays.
Any Help would be much appreciated.
Keith
Please can anyone let me know why this is not working? I have the following code as a module in my access database:
Public Function NetWorkhours(dteStart As Date, dteEnd As Date) As Single
Dim intGrossDays As Integer
Dim intGrossHours As Single
Dim dteCurrDate As Date
Dim i As Integer
Dim WorkDayStart As Date
Dim WorkDayend As Date
Dim nonWorkDays As Integer
Dim StartDayhours As Single
Dim EndDayhours As Single
NetWorkhours = 0
nonWorkDays = 0
'Calculate work day hours on 1st and last day
WorkDayStart = DateValue(dteEnd) + TimeValue("08:00am")
WorkDayend = DateValue(dteStart) + TimeValue("05:00pm")
StartDayhours = DateDiff("n", dteStart, WorkDayend)
EndDayhours = DateDiff("n", WorkDayStart, dteEnd)
'adjust for time entries outside of business hours
If StartDayhours < 0 Then
StartDayhours = 0
End If
If EndDayhours > 9 Then
EndDayhours = 9
End If
'Calculate total hours and days between start and end times
intGrossDays = DateDiff("d", (dteStart), (dteEnd))
intGrossHours = DateDiff("n", (dteStart), (dteEnd))
'count number of weekend days and holidays (from a table called "Holidays" that lists them)
For i = 0 To intGrossDays
dteCurrDate = dteStart + i
If Weekday(dteCurrDate, vbSaturday) < 3 Then nonWorkDays = nonWorkDays + 1
If Not IsNull(DLookup("[HolDate]", "tblHolidays", "[HolDate] = #" & Int(dteCurrDate) & "#")) Then nonWorkDays = nonWorkDays + 1
Next i
'Calculate number of work hours
Select Case intGrossDays
Case 0
'start and end time on same day
NetWorkhours = intGrossHours
Case 1
'start and end time on consecutive days
NetWorkhours = NetWorkhours + StartDayhours
NetWorkhours = NetWorkhours + EndDayhours
Case Is > 1
'start and end time on non consecutive days
NetWorkhours = NetWorkhours - (nonWorkDays * 1)
NetWorkhours = (intGrossDays - 1 - nonWorkDays) * 9
NetWorkhours = NetWorkhours + StartDayhours
NetWorkhours = NetWorkhours + EndDayhours
End Select
End Function
The actual function works fine, ALTHOUGH I have set-up an "Outage Calculator" for the line manager to use. It is really simple. Contains three text boxes that are not bound to a table.
Box 1 --- Date\Time_From
Box 2 --- Date\Time_To
Box 3 --- Networkhours([Date\Time_From],[Date\Time_To])
Default value for box 1 and box 2 is Now() so the Line manager just adjusts the time and dates to suit his needs eg.
Date From Date To Network Minutes
20/11/2009 08:50:22 20/11/2009 10:50:22 120
The problem that I have is it seems to be ignoring weekends for some reason. If I put in:
Date From Date To Network Minutes
14/11/2009 08:50:22 15/11/2009 08:50:22 499
The 14th and 15th are Saturday and Sunday. It should only raise minutes for weekdays.
Any Help would be much appreciated.
Keith