Hey All,
I am teaching myself VBA as I go so please forgive me if this is something obvious.
I have a calculated value that shows up properly when I send it to a message box but I can't seem to get it to populate the field in the form, and hence the record. First some background.
I have a form in which coworkers will enter start and end dates for internships. I've already borrowed a nice VBA function that calculates the number of workdays (M-F) between two dates. I then wrote a subroutine that calculates the total cost of the internship (ISP or Internship Support Payment). There are two different rates that are used based on another criteria. I have my code set up such that this will only run if the "Eligible for Funding" checkbox value = True. My code is as follows:
Code:
Function CalcTC(days, Rate) As Currency 'Calculates Total Cost (TC) for Internship
CalcTC = days * Rate
End Function
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
Public Sub ISP_TotalCost()
Dim FirstDay As Date, LastDay As Date, DayCount As Integer, Cohort As Integer, Rate As Integer, ISP As Currency
FirstDay = Me.First_Day.Value
LastDay = Me.Last_Day.Value
DayCount = CalcWorkdays(FirstDay, LastDay)
Cohort = Me.Cohort.Value
If Cohort = 2012 Then
ISP = CalcTC(DayCount, 200)
ElseIf Cohort <> 2012 Then
ISP = CalcTC(DayCount, 240)
End If
End Sub
'Making Total Funding field only visible when Participant is eligible for funding and inserting calculated ISP value into [Total Funding]
Private Sub Eligible_Click()
Call ISP_TotalCost
If Eligible.Value = True Then
Me.Total_Funding.Value = ISP
Me.Total_Funding.Visible = True
ElseIf Eligible.Value = False Then
Me.Total_Funding.Value = 0
Me.Total_Funding.Visible = False
End If
End Sub
As the last subroutine should suggest, I am trying to get the calculated value for ISP to populate the Total_Funding field in my form when the Eligible.Value = True. As I stated above, when I drop in a message box with the ISP value, it displays the correct value for each record whenever the Eligible check box is clicked but I can't seem to get the value into my field. I would greatly appreciate any suggestions and I apologize if this is something I will probably figure out in the next chapter of my VBA coding book.
Thanks,
Monterey_Manzer