what would be the best procedure to automate a calculation in a forms textbox. I was able to utilize the following code and have it work once the 4 textboxs had the values entered and I utilized the double click event. Should I be creating a module or using an event with after update or change: (I also realize my error of using a seperate date and time field)
Private Sub Combo125_DblClick(Cancel As Integer)
Dim Hours As String
Dim Minutes As String
On Error GoTo Err_Calculate_Click
'If there is no data in the date and time fields it errors
'If [Date Trail Laid] >= 1 Then
'If Not IsEmpty([Start Time Trail Laid]) Then
'If [Start Date of Trailing] >= 1 Then
'If Not IsEmpty([Start Time Trailing]) Then
Hours = TimeDiff("h", [Start Time Trail Laid], [Start Time Trailing])
Dim Calculation As String
Minutes = TimeDiff("n", [Start Time Trail Laid], [Start Time Trailing])
Calculation = Minutes - (60 * Hours)
[Age of Trail] = DateDiff("d", [Date Trail Laid], [Start Date of Trailing]) & " days " & Hours & " hrs " & Calculation & " min"
DoCmd.RunCommand acCmdSaveRecord
Me.[Combo125] = Me![Age of Trail]
Exit_Calculate_Click:
Exit Sub
'Else
Err_Calculate_Click:
MsgBox "Enter the required Values in the fields..."
'End If
'End If
End Sub
Public Function TimeDiff(strInterval As String, _
dtStartTime As Date, _
dtStopTime As Date) As Long
TimeDiff = DateDiff(strInterval, #12:00:00 AM#, _
Format(TimeValue(dtStartTime) - 1 - TimeValue(dtStopTime), _
"hh:nn:ss"))
End Function
Private Sub Combo125_DblClick(Cancel As Integer)
Dim Hours As String
Dim Minutes As String
On Error GoTo Err_Calculate_Click
'If there is no data in the date and time fields it errors
'If [Date Trail Laid] >= 1 Then
'If Not IsEmpty([Start Time Trail Laid]) Then
'If [Start Date of Trailing] >= 1 Then
'If Not IsEmpty([Start Time Trailing]) Then
Hours = TimeDiff("h", [Start Time Trail Laid], [Start Time Trailing])
Dim Calculation As String
Minutes = TimeDiff("n", [Start Time Trail Laid], [Start Time Trailing])
Calculation = Minutes - (60 * Hours)
[Age of Trail] = DateDiff("d", [Date Trail Laid], [Start Date of Trailing]) & " days " & Hours & " hrs " & Calculation & " min"
DoCmd.RunCommand acCmdSaveRecord
Me.[Combo125] = Me![Age of Trail]
Exit_Calculate_Click:
Exit Sub
'Else
Err_Calculate_Click:
MsgBox "Enter the required Values in the fields..."
'End If
'End If
End Sub
Public Function TimeDiff(strInterval As String, _
dtStartTime As Date, _
dtStopTime As Date) As Long
TimeDiff = DateDiff(strInterval, #12:00:00 AM#, _
Format(TimeValue(dtStartTime) - 1 - TimeValue(dtStopTime), _
"hh:nn:ss"))
End Function