3 work days prior to 18th

  • Thread starter Joker via AccessMonster.com
  • Start date
J

Joker via AccessMonster.com

Hello,

I have an odd request from work which is giving me quite a bit of trouble. I
need to figure out the 3rd Business day prior to the 18 in VBA. I have been
trying to manipulate the code "GetBusinessDay" by Arvin Meyer to no success
(below). Just a FYI.. This is going to be used in a user defined function.
Any help/thoughts will be GREATLY appreciated. Thanks.



Function GetBusinessDay(datStart As Date, intDayAdd As Variant)
On Error GoTo Error_Handler
'Adds/Subtracts the proper Business day skipping holidays and weekends
'Requires a table (tblHolidays) with a date field (HolidayDate)
'Arvin Meyer 05/26/98 revised 3/12/2002
'© Arvin Meyer 1998 - 2002 You may use this code in your application provided
author
' is given credit. This code may not be distributed as part of a collection
' without prior written permission. This header must remain intact.


Dim rst As DAO.Recordset
Dim DB As DAO.Database
'Dim strSQL As String

Set DB = CurrentDb
Set rst = DB.OpenRecordset("SELECT [HolidayDate] FROM tblHolidays",
dbOpenSnapshot)

If intDayAdd > 0 Then
Do While intDayAdd > 0
datStart = datStart + 1
rst.FindFirst "[HolidayDate] = #" & datStart & "#"
If Weekday(datStart) <> vbSunday And Weekday(datStart) <> vbSaturday
Then
If rst.NoMatch Then intDayAdd = intDayAdd - 1
End If
Loop

ElseIf intDayAdd < 0 Then

Do While intDayAdd < 0
datStart = datStart - 1
rst.FindFirst "[HolidayDate] = #" & datStart & "#"
If Weekday(datStart) <> vbSunday And Weekday(datStart) <> vbSaturday
Then
If rst.NoMatch Then intDayAdd = intDayAdd + 1
End If
Loop

End If

GetBusinessDay = datStart

Exit_Here:
rst.Close
Set rst = Nothing
Set DB = Nothing
Exit Function

Error_Handler:
MsgBox Err.Number & ": " & Err.Description
Resume Exit_Here
End Function
 
R

raskew via AccessMonster.com

Hi-

Try copying this to a module, then call it per the instructions, e.g.
'? upbusdays3(#2/18/07#,3,False)
'2/14/07

Function UpBusDays3(pStart As Date, _
pnum As Integer, _
Optional pAdd As Boolean = True) As Date
'*******************************************
'Purpose: Add or subtract business days
' from a date
'Coded by: raskew
'Inputs: +) ? UpBusDays3(#2/17/06#, 3, True)
' -) ? UpBusDays3(#2/22/06#, 3, False)
'Output: +) 2/22/06
' -) 2/17/06
'*******************************************

Dim dteHold As Date
Dim I As Integer
Dim n As Integer

dteHold = pStart
n = pnum
For I = 1 To n
If pAdd Then 'add days
dteHold = dteHold + IIf(WeekDay(dteHold) > 5, 9 - WeekDay(dteHold),
1)
Else 'subtract days
'this isn't working for Sunday
dteHold = dteHold - IIf(WeekDay(dteHold) < 3, Choose(WeekDay
(dteHold), 2, 3), 1)
End If
Next I
UpBusDays3 = dteHold

End Function

HTH - Bob
Joker said:
Hello,

I have an odd request from work which is giving me quite a bit of trouble. I
need to figure out the 3rd Business day prior to the 18 in VBA. I have been
trying to manipulate the code "GetBusinessDay" by Arvin Meyer to no success
(below). Just a FYI.. This is going to be used in a user defined function.
Any help/thoughts will be GREATLY appreciated. Thanks.

Function GetBusinessDay(datStart As Date, intDayAdd As Variant)
On Error GoTo Error_Handler
'Adds/Subtracts the proper Business day skipping holidays and weekends
'Requires a table (tblHolidays) with a date field (HolidayDate)
'Arvin Meyer 05/26/98 revised 3/12/2002
'© Arvin Meyer 1998 - 2002 You may use this code in your application provided
author
' is given credit. This code may not be distributed as part of a collection
' without prior written permission. This header must remain intact.

Dim rst As DAO.Recordset
Dim DB As DAO.Database
'Dim strSQL As String

Set DB = CurrentDb
Set rst = DB.OpenRecordset("SELECT [HolidayDate] FROM tblHolidays",
dbOpenSnapshot)

If intDayAdd > 0 Then
Do While intDayAdd > 0
datStart = datStart + 1
rst.FindFirst "[HolidayDate] = #" & datStart & "#"
If Weekday(datStart) <> vbSunday And Weekday(datStart) <> vbSaturday
Then
If rst.NoMatch Then intDayAdd = intDayAdd - 1
End If
Loop

ElseIf intDayAdd < 0 Then

Do While intDayAdd < 0
datStart = datStart - 1
rst.FindFirst "[HolidayDate] = #" & datStart & "#"
If Weekday(datStart) <> vbSunday And Weekday(datStart) <> vbSaturday
Then
If rst.NoMatch Then intDayAdd = intDayAdd + 1
End If
Loop

End If

GetBusinessDay = datStart

Exit_Here:
rst.Close
Set rst = Nothing
Set DB = Nothing
Exit Function

Error_Handler:
MsgBox Err.Number & ": " & Err.Description
Resume Exit_Here
End Function
 
S

steinmetzw42 via AccessMonster.com

It worked Great raskew! Thank you!!!
Hi-

Try copying this to a module, then call it per the instructions, e.g.
'? upbusdays3(#2/18/07#,3,False)
'2/14/07

Function UpBusDays3(pStart As Date, _
pnum As Integer, _
Optional pAdd As Boolean = True) As Date
'*******************************************
'Purpose: Add or subtract business days
' from a date
'Coded by: raskew
'Inputs: +) ? UpBusDays3(#2/17/06#, 3, True)
' -) ? UpBusDays3(#2/22/06#, 3, False)
'Output: +) 2/22/06
' -) 2/17/06
'*******************************************

Dim dteHold As Date
Dim I As Integer
Dim n As Integer

dteHold = pStart
n = pnum
For I = 1 To n
If pAdd Then 'add days
dteHold = dteHold + IIf(WeekDay(dteHold) > 5, 9 - WeekDay(dteHold),
1)
Else 'subtract days
'this isn't working for Sunday
dteHold = dteHold - IIf(WeekDay(dteHold) < 3, Choose(WeekDay
(dteHold), 2, 3), 1)
End If
Next I
UpBusDays3 = dteHold

End Function

HTH - Bob
[quoted text clipped - 57 lines]
Resume Exit_Here
End Function
 

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