Quote:
Originally Posted by rajeevs
I have a form with 2 controls, both as short time formats. I need to get the sum of the time on a differnt control.
eg:
tme1 as 23:00
tme2 as 00:11
in control tme3 i need the answer 23:11
First off, you need to understand that these are not "time" fields, which in Access VBA are actually called
Date/Time fields! You cannot add two true times together any more than you can add Monday + Tuesday together or 7/12/2009 + 7/13/2009.
"Time" fields must, by definition, also include a date, even if it's not displayed. If you only enter the "time" portion of a Date/Time field, Access will automatically tack on the current date. Your fields are actually storing hours and minutes, formatted so that it looks like a "time."
Because you cannot be sure of the order that your user enters the data, you need to do this calculation in the AfterUpdate event of both tme1 and tme2.
The first question we need to address is how are tme1 and tme2 defined in the underlying table? Because of they way you're displaying them, they need to be defined as Text fields.
The next question is what do you want to do if one of these fields does not contain data? Enter the value of the single populated field in tme3, enter a zero or leave the field Null (blank.)
The following code does what you want if both fields are populated with data. If one or the other aren't populated, it sets tme3 to Null.
-
Private Sub tme2_AfterUpdate()
-
Dim Minutes1 As Integer
-
Dim Minutes2 As Integer
-
Dim Minutes3 As Integer
-
-
If Not IsNull(Me.tme1) And Not IsNull(Me.tme2) Then
-
Minutes1 = (Val(Left(Me.tme1, InStr(Me.tme1, ":"))) * 60) + Val(Mid(Me.tme1, InStr(Me.tme1, ":") + 1))
-
Minutes2 = (Val(Left(Me.tme2, InStr(Me.tme2, ":"))) * 60) + Val(Mid(Me.tme2, InStr(Me.tme2, ":") + 1))
-
Minutes3 = Minutes1 + Minutes2
-
Me.tme3 = (Minutes3 \ 60) & ":" & Minutes3 Mod 60
-
Else
-
Me.tme3 = Null
-
End If
-
End Sub
I put it in the tme2 AfterUpdate event, as you asked, but as I said above, it should also be in the AfterUpdate for tme1, as well.
If you are not going to store tme3 in the underlying table (and you really shouldn't store this kind of calculated field) you will also need to place the same code in the Form_Current event so that it is re-calculated when you move from record to record.
Linq
;0)>