# Apply variables to function (1 Viewer)

#### Drand

##### Registered User.
Hi

I am developing a budget model.

In one area there are 3 alternative approaches to calculating budget income for the year.

It is one of these options that has me stumped. It works on a calculation of income per day (all days are the same) x the number of working days in each month. I found a useful function to calculate the working days as follows;

Code:
``````Public Function CalcWorkdays(StartDate, EndDate) As Integer

Dim LTotalDays As Integer
Dim LSaturdays As Integer
Dim LSundays As Integer

On Error GoTo Err_Execute

CalcWorkdays = 0

If IsDate(StartDate) And IsDate(EndDate) Then
If EndDate <= StartDate Then
CalcWorkdays = 0
Else
LTotalDays = DateDiff("d", StartDate - 1, EndDate)
LSaturdays = DateDiff("ww", StartDate - 1, EndDate, 7)
LSundays = DateDiff("ww", StartDate - 1, EndDate, 1)

'Workdays is the elapsed days excluding Saturdays and Sundays
CalcWorkdays = LTotalDays - LSaturdays - LSundays

End If

End If

Exit Function

Err_Execute:
'If error occurs, return 0
CalcWorkdays = 0

End Function``````

I would like to be able to call this function into my form code where StartDate and EndDate are variables determined from other input.

Is this possible?

Many thanks

#### arnelgp

##### ..forever waiting... waiting for jellybean!
result from the function.

you add code (AfterUpdate event) to each textbox (startDate/endDate):

Code:
``````private sub startDate_AfterUpdate()
Call fnCalc()
end sub

private sub endDate_AfterUpdate()
Call fnCalc()
end sub

private function fnCalc()
Dim var As Variant
var = Null
If IsDate([startDate]) And IsDate([endDate]) Then
var = CalcWorkdays([startDate], [endDate]) * [incomePerDayField]
End If
Me!txtCalc = var
end function``````

• azhar2006 and oleronesoftwares

#### Drand

##### Registered User.
Thank you so much for that. Perfect!

• arnelgp

Replies
2
Views
106
Replies
2
Views
660
Replies
5
Views
297
Replies
3
Views
531
Replies
38
Views
931