I have the following module which correctly calculates the number of working days in a given month and accounts for Bank holidays by referencing a table within my DB. this all works fine when you run it direct from the module.
However I am struggling to change the module so that it can be referenced from any form in the DB. I want to pass the BegDate value in from a field on the form. I tried substituting the green lines below and putting "=Work_Days([Form],[Control])" in the On load Event of the form I am working with but it doesn't like it!
The module code is :
Function Work_Days()
'Public Function Work_Days(frm As Form, VarCtlName As Control)
Dim BegDate As Variant, EndDate As Variant
' Note that this function does account for holidays.
Dim WholeWeeks As Variant
Dim DateCnt As Variant
Dim EndDays As Integer
Dim bhol As Variant
Dim mydb As Database
Set mydb = DBEngine.Workspaces(0).Databases(0)
'BegDate = frm(VarCtlName)
BegDate = DateValue("01/05/04")
EndDate = DateAdd("m", 1, BegDate)
WholeWeeks = DateDiff("w", BegDate, EndDate)
DateCnt = DateAdd("ww", WholeWeeks, BegDate)
EndDays = 0
Do While DateCnt < EndDate
bhol = DLookup("Date2", "BankHolidays", "[Date2] =#" & Format(DateCnt, "Long Date") & "#")
'MsgBox (bhol)
If Format(DateCnt, "ddd") <> "Sun" And Format(DateCnt, "ddd") <> "Sat" And bhol = " " Then
EndDays = EndDays + 1
End If
DateCnt = DateAdd("d", 1, DateCnt)
Loop
Work_Days = WholeWeeks * 5 + EndDays
MsgBox (Work_Days)
End Function
Can anyone advise me how to do this, variables are where my VBA coding gets weak! Thanks
However I am struggling to change the module so that it can be referenced from any form in the DB. I want to pass the BegDate value in from a field on the form. I tried substituting the green lines below and putting "=Work_Days([Form],[Control])" in the On load Event of the form I am working with but it doesn't like it!
The module code is :
Function Work_Days()
'Public Function Work_Days(frm As Form, VarCtlName As Control)
Dim BegDate As Variant, EndDate As Variant
' Note that this function does account for holidays.
Dim WholeWeeks As Variant
Dim DateCnt As Variant
Dim EndDays As Integer
Dim bhol As Variant
Dim mydb As Database
Set mydb = DBEngine.Workspaces(0).Databases(0)
'BegDate = frm(VarCtlName)
BegDate = DateValue("01/05/04")
EndDate = DateAdd("m", 1, BegDate)
WholeWeeks = DateDiff("w", BegDate, EndDate)
DateCnt = DateAdd("ww", WholeWeeks, BegDate)
EndDays = 0
Do While DateCnt < EndDate
bhol = DLookup("Date2", "BankHolidays", "[Date2] =#" & Format(DateCnt, "Long Date") & "#")
'MsgBox (bhol)
If Format(DateCnt, "ddd") <> "Sun" And Format(DateCnt, "ddd") <> "Sat" And bhol = " " Then
EndDays = EndDays + 1
End If
DateCnt = DateAdd("d", 1, DateCnt)
Loop
Work_Days = WholeWeeks * 5 + EndDays
MsgBox (Work_Days)
End Function
Can anyone advise me how to do this, variables are where my VBA coding gets weak! Thanks