J
JessiRight77
Hello... I need to calculate the number of workdays, and found some
useful code by Arvin Meyer.
I have placed two fields on my form (StartDate and EndDate), along with
an unbound textbox (txtCountDays) where I want to display the number of
workdays. I have also followed Mr. Meyer's instructions to create a
tblHolidays.
My question... and it's so basic that I'm almost embarrassed to ask...
How do I link the textbox to the function code??? I went into the
properties of the textbox, clicked the "Event" tab, and selected "Event
Procedure" in the AfterUpdate event. Then I clicked the Build button
and pasted the function code there (within the Private Sub
txtCountDays_AfterUpdate() and End Sub lines).
It doesn't work, however... nothing is displayed in the textbox after I
enter two dates, so I must be doing something wrong. Does anyone know
where I'm messing up?
Thanks,
Jessi
The code that I pasted in its entirety is as follows:
______________________________
Option Compare Database
Private Sub txtCountDays_AfterUpdate()
Public Function WorkingDays2(StartDate As Date, EndDate As Date) As
Integer
'....................................................................
' Name: WorkingDays2
' Inputs: StartDate As Date
' EndDate As Date
' Returns: Integer
' Author: Arvin Meyer
' Date: May 5,2002
' Comment: Accepts two dates and returns the number of weekdays
between them
' Note that this function has been modified to account for holidays. It
requires a
' table named tblHolidays with a field named HolidayDate.
'....................................................................
On Error GoTo Err_WorkingDays2
Dim intCount As Integer
Dim rst As DAO.Recordset
Dim DB As DAO.Database
Set DB = CurrentDb
Set rst = DB.OpenRecordset("SELECT [HolidayDate] FROM tblHolidays",
dbOpenSnapshot)
'StartDate = StartDate + 1
'To count StartDate as the 1st day comment out the line above
intCount = 0
Do While StartDate <= EndDate
rst.FindFirst "[HolidayDate] = #" & StartDate & "#"
If Weekday(StartDate) <> vbSunday And Weekday(StartDate) <> vbSaturday
Then
If rst.NoMatch Then intCount = intCount + 1
End If
StartDate = StartDate + 1
Loop
WorkingDays2 = intCount
Exit_WorkingDays2:
Exit Function
Err_WorkingDays2:
Select Case Err
Case Else
MsgBox Err.Description
Resume Exit_WorkingDays2
End Select
End Function
End Sub
useful code by Arvin Meyer.
I have placed two fields on my form (StartDate and EndDate), along with
an unbound textbox (txtCountDays) where I want to display the number of
workdays. I have also followed Mr. Meyer's instructions to create a
tblHolidays.
My question... and it's so basic that I'm almost embarrassed to ask...
How do I link the textbox to the function code??? I went into the
properties of the textbox, clicked the "Event" tab, and selected "Event
Procedure" in the AfterUpdate event. Then I clicked the Build button
and pasted the function code there (within the Private Sub
txtCountDays_AfterUpdate() and End Sub lines).
It doesn't work, however... nothing is displayed in the textbox after I
enter two dates, so I must be doing something wrong. Does anyone know
where I'm messing up?
Thanks,
Jessi
The code that I pasted in its entirety is as follows:
______________________________
Option Compare Database
Private Sub txtCountDays_AfterUpdate()
Public Function WorkingDays2(StartDate As Date, EndDate As Date) As
Integer
'....................................................................
' Name: WorkingDays2
' Inputs: StartDate As Date
' EndDate As Date
' Returns: Integer
' Author: Arvin Meyer
' Date: May 5,2002
' Comment: Accepts two dates and returns the number of weekdays
between them
' Note that this function has been modified to account for holidays. It
requires a
' table named tblHolidays with a field named HolidayDate.
'....................................................................
On Error GoTo Err_WorkingDays2
Dim intCount As Integer
Dim rst As DAO.Recordset
Dim DB As DAO.Database
Set DB = CurrentDb
Set rst = DB.OpenRecordset("SELECT [HolidayDate] FROM tblHolidays",
dbOpenSnapshot)
'StartDate = StartDate + 1
'To count StartDate as the 1st day comment out the line above
intCount = 0
Do While StartDate <= EndDate
rst.FindFirst "[HolidayDate] = #" & StartDate & "#"
If Weekday(StartDate) <> vbSunday And Weekday(StartDate) <> vbSaturday
Then
If rst.NoMatch Then intCount = intCount + 1
End If
StartDate = StartDate + 1
Loop
WorkingDays2 = intCount
Exit_WorkingDays2:
Exit Function
Err_WorkingDays2:
Select Case Err
Case Else
MsgBox Err.Description
Resume Exit_WorkingDays2
End Select
End Function
End Sub