Ok. The problem with your code is that the Calculate sub doesn't have know what values should be in: timeStart, timeFinish, LunchStart, LunchFinish. Because you declared them inside of your click proceedure it holds this information to itself as private and doesn't share it with the Calculate sub.
There are three ways to fix this.
1. Declare the variables as Public, so that no matter which proceedure calls them the information is always the same. (I personally don't recommend)
2. Re-declare the variables in every proceedure. (probably counter productive to having the proceedure seperate in the first place)
3. Make the Calculate proceedure capable of receiving the variables from whatever proceedure calls it.
Let's go with option three.
Like functions, subs can have variables passed to them (they just can't pass variables back).
So to just fix your code lets try this:
Public Sub Command126_Click()
Dim timeStart As Date
Dim timeFinish As Date
Dim Day As Integer
Dim Lunch As Integer
Dim LunchStart As Date
Dim LunchFinish As Date
Dim Total As Integer
Dim Rate As Integer
Dim optionRate As Integer
Dim ErrorMsg
timeStart = Me.Start
timeFinish = Me.Finish
LunchStart = Me.LunchStart
LunchFinish = Me.LunchFinish
optionRate = Me.Frame42
'=============================
If optionRate = 1 Then
If Rates_Subform!Rate1 > 0 Then
Rate = Rates_Subform!Rate1
Me.Rate = Rates_Subform!Rate1
'pass the four variables to the sub.
Calculate timeStart, timeFinish, LunchStart, LunchFinish
Else
ErrorMsg = MsgBox("Warning! No vlaue has been entered for Rate of Pay", vbCritical, "Warning")
End If
End If
If optionRate = 2 Then
If Rates_Subform!Rate2 > 0 Then
Rate = Rates_Subform!Rate2
Me.Rate = Rates_Subform!Rate2
Else
ErrorMsg = MsgBox("Warning! No vlaue has been entered for Rate of Pay", vbCritical, "Warning")
End If
End If
If optionRate = 3 Then
If Rates_Subform!Rate3 > 0 Then
Rate = Rates_Subform!Rate3
Me.Rate = Rates_Subform!Rate3
Else
ErrorMsg = MsgBox("Warning! No vlaue has been entered for Rate of Pay", vbCritical, "Warning")
End If
End If
If optionRate = 4 Then
If Rates_Subform!RateOther > 0 Then
Rate = Rates_Subform!RateOther
Me.Rate = Rates_Subform!RateOther
Else
ErrorMsg = MsgBox("Warning! No vlaue has been entered for Rate of Pay", vbCritical, "Warning")
End If
End If
'===============================
End Sub
'*************************************************
***
Public Sub Calculate(timeStart As Date, timeFinish as Date, _
LunchStart As Date, LunchFinish as Date)
Dim Day As Integer
Dim Lunch As Integer
Dim Total As Integer
Dim Rate As Integer
Dim optionRate As Integer
Dim ErrorMsg
Day = DateDiff("n", timeStart, timeFinish)
Lunch = DateDiff("n", LunchStart, LunchFinish)
Total = (Day - Lunch) / 60
TotalHours = Total
Me.SubTotal = Total * Rate
Me.Description = "Pay=£" & Total * Rate & " day=" & Day & " Lunch=" & Lunch
End Sub
This is just a quick look and fix. Hopefully it works.