I am calculating working days in a report and would like to have the result auto populate to a text box when the report is generated. Since I do not know how to write the VB for this, I am pasting in the following code (found by many google searches). How do I attach this code result to the text box on Form Load?
Option Compare Database
Option Explicit
Public Function Calculate_Working_Days(ByVal dDate1 As Date, ByVal dDate2 As Date, _
ByVal FirstDayOfWeek As DayConstants, Optional WeekLengh As Integer = 5) As Long
' Calculating how many working days in the given interval'
Dim i As Integer
Dim intDayDifference As Integer
Dim dSmallestDate As Date
'Looking for the smallest date from the 2 we give to the function'
If DateDiff("d", dDate1, dDate2) < 0 Then
dSmallestDate = dDate2
Else
dSmallestDate = dDate1
End If
'Geting the interval between the dates in days'
intDayDifference = DateDiff("d", dDate1, dDate2)
'Going trough the interval and looking for the working days'
For i = 0 To Abs(intDayDifference)
If Weekday(dSmallestDate + i, FirstDayOfWeek) < WeekLengh + 1 Then ' looking for the first 5 days from the given'
' day as begining of the week if it is true '
' we count them as working '
Calculate_Working_Days = Calculate_Working_Days + 1 ' if we found one we sum 1 more'
End If
Next i
End Function
Thank you!
Option Compare Database
Option Explicit
Public Function Calculate_Working_Days(ByVal dDate1 As Date, ByVal dDate2 As Date, _
ByVal FirstDayOfWeek As DayConstants, Optional WeekLengh As Integer = 5) As Long
' Calculating how many working days in the given interval'
Dim i As Integer
Dim intDayDifference As Integer
Dim dSmallestDate As Date
'Looking for the smallest date from the 2 we give to the function'
If DateDiff("d", dDate1, dDate2) < 0 Then
dSmallestDate = dDate2
Else
dSmallestDate = dDate1
End If
'Geting the interval between the dates in days'
intDayDifference = DateDiff("d", dDate1, dDate2)
'Going trough the interval and looking for the working days'
For i = 0 To Abs(intDayDifference)
If Weekday(dSmallestDate + i, FirstDayOfWeek) < WeekLengh + 1 Then ' looking for the first 5 days from the given'
' day as begining of the week if it is true '
' we count them as working '
Calculate_Working_Days = Calculate_Working_Days + 1 ' if we found one we sum 1 more'
End If
Next i
End Function
Thank you!