Hi,
I have designed a form to calculate total downtime in a shift. My form has 4 separate text boxes to hold Start_Date, Start_Time,Stop_Date and Stop_Time. The downtime is calculated by subtracting Stop_Date and Stop_Time from Start_Date and Start_Time and there is a text box assigned for it called txtTotalTime. Here is the code I came up with. The date is entered in mm/dd/yyyy format and the time is entered in military format(13:00, 14:00...) .The value comes up wrong and I frequently get negative numbers. What is the problem? Thanks in advance for any help!
Private Sub Start_Time_BeforeUpdate(Cancel As Integer)
Dim StartDate As String
Dim EndDate As String
Dim dStartDate As Date
Dim dEndDate As Date
StartDate = Format(Start_Date, "mm/dd/yyyy" & " " & Format(Start_Time, "hh:mm AMPM"
dStartDate = CDate(StartDate)
EndDate = Format(Stop_Date, "mm/dd/yyyy" & " " & Format(Stop_Time, "hh:mm AMPM"
dEndDate = CDate(EndDate)
txtTotalTime.Value = DateDiff("n", dStartDate, dEndDate)
End Sub
I have designed a form to calculate total downtime in a shift. My form has 4 separate text boxes to hold Start_Date, Start_Time,Stop_Date and Stop_Time. The downtime is calculated by subtracting Stop_Date and Stop_Time from Start_Date and Start_Time and there is a text box assigned for it called txtTotalTime. Here is the code I came up with. The date is entered in mm/dd/yyyy format and the time is entered in military format(13:00, 14:00...) .The value comes up wrong and I frequently get negative numbers. What is the problem? Thanks in advance for any help!
Private Sub Start_Time_BeforeUpdate(Cancel As Integer)
Dim StartDate As String
Dim EndDate As String
Dim dStartDate As Date
Dim dEndDate As Date
StartDate = Format(Start_Date, "mm/dd/yyyy" & " " & Format(Start_Time, "hh:mm AMPM"
dStartDate = CDate(StartDate)
EndDate = Format(Stop_Date, "mm/dd/yyyy" & " " & Format(Stop_Time, "hh:mm AMPM"
dEndDate = CDate(EndDate)
txtTotalTime.Value = DateDiff("n", dStartDate, dEndDate)
End Sub