I am programming an Access Payment Salary DB, and salaries should be paid the 14th every month. If it is a weekend, or a holiday, then is should be 13th, 12th, 11th, etc (the last workday before the 14th). Our weekends is on Friday and Saturday - Weekday(dteDate, vbSunday)
My challenge is that I don't get the correct value when the VBA does the calculation. First it checks if it is a weekend, then reduces one or two days (depends if it is a Saturday or Sunday), and then it should test if it is a holiday ([tblHoliday].[tblHoliday]). If yes, then reduce it with one day - until it is not a holiday again. Then it shall test if it is a weekend, again, if yes, reduce correct amount of days, and then test if it is a holiday again. If not, then return the date.
I am using this in the Compare Database
Private Sub PeriodeEnd_Text_AfterUpdate()
Dim dtDate As Date
Dim testDate As Date
dtDate = dhLastDayInMonth(Me.PeriodeEnd_Text) + 14
testDate = LastWorkDay(dtDate)
Me.PaymentDay_Text = testDate
End Sub
And have this in a module
Function dhLastDayInMonth(Optional dtmDate As Date = 0) As Date
' Return the last day in the specified month.
If dtmDate = 0 Then
' Did the caller pass in a date? If not, use
' the current date.
dtmDate = Date
End If
dhLastDayInMonth = DateSerial(Year(dtmDate), _
Month(dtmDate) + 1, 0)
End Function
Public Function LastWorkDay(Dt As Date) As Date
Dim Searching As Boolean
Searching = True
Do While Searching
If Weekday(LastWorkDay, vbSunday) > 5 Then
'-- Weekend day, back up a day
LastWorkDay = LastWorkDay - 1
Else
If Weekday(LastWorkDay, vbSunday) > 5 Or _
Not IsNull(DLookup("[HolidayDate]", "tblHoliday", _
"[HolidayDate] = " & Format(LastWorkDay, "\#mm\/dd\/yyyy\#;;;\N\u\l\l"))) Then
'-- The above Format of LastWorkday works with US or UK dates!
LastWorkDay = LastWorkDay - 1
Else
'-- The search is over
Searching = False
End If
End If
Loop
End Function
Dtis the parameter of yourLastWorkDayfunction, but you never use it. You probably need to include the lineLastWorkDay = Dtat the beginning of your loop or set it to useDtthroughout the function and then setLastWorkDay = Dtat the end. - OpiesDad