Hi, I’ve got a DB that amongst other things creates a forwarding list for work orders that have to be submitted 2 working days in advance. The DB in question has a table called “Holidays” that contains two fields: [Holidays] which is a date/time field and [Title] which is a simple text field.
The code below is supposed to exclude weekend days from working days as well as those holidays that are updated in the “Holidays” table. The idea is that the user always gets a forwarding notification list two working days in advance.
Here’s the code:
Public Function DeltaDays(StartDate As Date, EndDate As Date) As Integer
'Get the number of workdays between the given dates
Dim dbs As Database
Dim rstHolidays As Recordset
Dim Idx As Long
Dim MyDate As Date
Dim NumDays As Long
Dim strCriteria As String
Dim NumSgn As String * 1
Dim TotalHolidays As String
TotalHolidays = 0
Set dbs = CurrentDb
Set rstHolidays = dbs.OpenRecordset("Holidays", dbOpenDynaset)
NumSgn = Chr(35)
MyDate = Format(StartDate, "Short Date")
For Idx = CLng(StartDate) To CLng(EndDate)
Select Case (WeekDay(MyDate))
Case Is = 1 'Sunday
'Do Nothing, it is NOT a Workday
TotalHolidays = TotalHolidays + 1
Case Is = 7 'Saturday
'Do Nothing, it is NOT a Workday
TotalHolidays = TotalHolidays + 1
Case Else 'Normal Workday
strCriteria = "[HoliDays] = " & NumSgn & Format$(MyDate, "dd-mm-yyyy") & NumSgn 'Thanks to "RoyVidar" 2/18/04
rstHolidays.FindFirst strCriteria
If (rstHolidays.NoMatch) Then
NumDays = NumDays + 1
Else
'Do Nothing, it is NOT a Workday
TotalHolidays = TotalHolidays + 1
End If
End Select
MyDate = DateAdd("d", 1, MyDate)
Next Idx
DeltaDays = NumDays
Forms!Taxi_Service!TrueHoliday = TotalHolidays
End Function
I’ve got a date field on a from that executes the above “After Update”
Here’s the AfterUpdate code:
Private Sub Itinerario_Fecha_1_AfterUpdate()
Dim Calculate_Days As String
Dim SMyDate, SMyWeekDay
If Update = True Then
Else
Creado = Forms!Active_Information!UserId & " / " & Forms!Active_Information!Text49 & " - " & Now()
End If
Calculate_Days = DLookup("Días", "Setup")
Days = Itinerario_Fecha_1 - Calculate_Days
Dim HOLMynum As String
Dim HOLPosition As String
Dim StopDate
HOLPosition = 0
HOLMynum = 21
Do Until HOLMynum = 0
MyDate = Days
MyWeekDay = WeekDay(MyDate)
NoDate = MyWeekDay
SMyDate = Itinerario_Fecha_1
SMyWeekDay = WeekDay(SMyDate)
SNoDate = SMyWeekDay
Delta = DeltaDaysVisa([Days], [Itinerario_Fecha_1])
Days = Itinerario_Fecha_1 - Calculate_Days - TrueHoliday
HOLMynum = HOLMynum - 1
HOLPosition = HOLPosition + 1
If StopDate = 1 Then HOLPosition = 21
If HOLPosition = 21 Then Exit Do
Loop
[Fecha de Impresión] = Days
DoCmd.RunCommand acCmdSaveRecord
End Sub
Note: Calculate days = 2
Problem: While the weekend days are always excluded, the code does not always recognize the days form the “Holidays” table.
I hope that the above explanation is clear enough for someone out there who can give me a hand.
Thanks,
The code below is supposed to exclude weekend days from working days as well as those holidays that are updated in the “Holidays” table. The idea is that the user always gets a forwarding notification list two working days in advance.
Here’s the code:
Public Function DeltaDays(StartDate As Date, EndDate As Date) As Integer
'Get the number of workdays between the given dates
Dim dbs As Database
Dim rstHolidays As Recordset
Dim Idx As Long
Dim MyDate As Date
Dim NumDays As Long
Dim strCriteria As String
Dim NumSgn As String * 1
Dim TotalHolidays As String
TotalHolidays = 0
Set dbs = CurrentDb
Set rstHolidays = dbs.OpenRecordset("Holidays", dbOpenDynaset)
NumSgn = Chr(35)
MyDate = Format(StartDate, "Short Date")
For Idx = CLng(StartDate) To CLng(EndDate)
Select Case (WeekDay(MyDate))
Case Is = 1 'Sunday
'Do Nothing, it is NOT a Workday
TotalHolidays = TotalHolidays + 1
Case Is = 7 'Saturday
'Do Nothing, it is NOT a Workday
TotalHolidays = TotalHolidays + 1
Case Else 'Normal Workday
strCriteria = "[HoliDays] = " & NumSgn & Format$(MyDate, "dd-mm-yyyy") & NumSgn 'Thanks to "RoyVidar" 2/18/04
rstHolidays.FindFirst strCriteria
If (rstHolidays.NoMatch) Then
NumDays = NumDays + 1
Else
'Do Nothing, it is NOT a Workday
TotalHolidays = TotalHolidays + 1
End If
End Select
MyDate = DateAdd("d", 1, MyDate)
Next Idx
DeltaDays = NumDays
Forms!Taxi_Service!TrueHoliday = TotalHolidays
End Function
I’ve got a date field on a from that executes the above “After Update”
Here’s the AfterUpdate code:
Private Sub Itinerario_Fecha_1_AfterUpdate()
Dim Calculate_Days As String
Dim SMyDate, SMyWeekDay
If Update = True Then
Else
Creado = Forms!Active_Information!UserId & " / " & Forms!Active_Information!Text49 & " - " & Now()
End If
Calculate_Days = DLookup("Días", "Setup")
Days = Itinerario_Fecha_1 - Calculate_Days
Dim HOLMynum As String
Dim HOLPosition As String
Dim StopDate
HOLPosition = 0
HOLMynum = 21
Do Until HOLMynum = 0
MyDate = Days
MyWeekDay = WeekDay(MyDate)
NoDate = MyWeekDay
SMyDate = Itinerario_Fecha_1
SMyWeekDay = WeekDay(SMyDate)
SNoDate = SMyWeekDay
Delta = DeltaDaysVisa([Days], [Itinerario_Fecha_1])
Days = Itinerario_Fecha_1 - Calculate_Days - TrueHoliday
HOLMynum = HOLMynum - 1
HOLPosition = HOLPosition + 1
If StopDate = 1 Then HOLPosition = 21
If HOLPosition = 21 Then Exit Do
Loop
[Fecha de Impresión] = Days
DoCmd.RunCommand acCmdSaveRecord
End Sub
Note: Calculate days = 2
Problem: While the weekend days are always excluded, the code does not always recognize the days form the “Holidays” table.
I hope that the above explanation is clear enough for someone out there who can give me a hand.
Thanks,