Connecting Tech Pros Worldwide Forums | Help | Site Map

Calculating Military Time

Newbie
 
Join Date: Oct 2007
Posts: 30
#1: Jul 4 '08
Hi!
I used Query in Access to calculate the time in and out of the employee of course to get the total minutes, however i've been encountered a problem. when i put for timeIn=22:00 and for time Out =24:00 the total mins display like this:" -1320 ", which actually wrong. it should be "120" mins instead of "-1320". how i can fix this problem??? i already set those data into short time format.
Please help me.... thank you....

salimudheen's Avatar
Newbie
 
Join Date: Jan 2007
Posts: 14
#2: Jul 4 '08

re: Calculating Military Time


Hey,
Try this simple method,

Option Compare Database
Dim minsIn As Long
Dim minsOut As Long
Private Sub TimeIn_Exit(Cancel As Integer)
If TimeIn = 0# Then
minsIn = 1440
Else
minsIn = VBA.DatePart("h", TimeIn) * 60
minsIn = minsIn + VBA.DatePart("n", TimeIn)
End If
End Sub
Private Sub TimeOut_Exit(Cancel As Integer)
If TimeOut = 0# Then
minsOut = 1440
Else
minsOut = VBA.DatePart("h", TimeOut) * 60
minsOut = minsOut + VBA.DatePart("n", TimeOut)

End If
End Sub

Private Sub TotalTime_Enter()
TotalTime = minsOut - minsIn
End Sub
ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,214
#3: Jul 4 '08

re: Calculating Military Time


  1. Midnight should be expressed as 00:00 and not 24:00.
  2. You must allow for the post-Midnight scenario by adding a Day in Minutes (1,440).
  3. Based on a [StartTime] and [EndTime] Fields, and a Table named tblTest, the following Query using a simple Public Function should solve the problem:
Expand|Select|Wrap|Line Numbers
  1. SELECT tblTest.StartTime, tblTest.EndTime, fCalcDiffInMinutes([StartTime],[EndTime]) AS [Interval]
  2. FROM tblTest;
Expand|Select|Wrap|Line Numbers
  1. Public Function fCalcDiffInMinutes(dteStart As Date, dteFinish As Date) As Long
  2. Dim intDifference As Integer
  3.  
  4. intDifference = DateDiff("n", dteStart, dteFinish)
  5.  
  6. If intDifference < 0 Then
  7.   fCalcDiffInMinutes = intDifference + 1440
  8. Else
  9.   fCalcDiffInMinutes = intDifference
  10. End If
  11. End Function
Test Data (tblTest):
Expand|Select|Wrap|Line Numbers
  1. StartTime    EndTime
  2. 12:00         13:00
  3. 22:00         23:50
  4.  6:00          8:00
  5. 22:00          0:00
  6. 23:00          4:00
  7. 12:00         13:30
OUTPUT:
Expand|Select|Wrap|Line Numbers
  1. StartTime    EndTime    Interval
  2. 12:00         13:00           60
  3. 22:00         23:50          110
  4.  6:00          8:00          120
  5. 22:00          0:00          120
  6. 23:00          4:00          300
  7. 12:00         13:30           90
Newbie
 
Join Date: Oct 2007
Posts: 30
#4: Jul 7 '08

re: Calculating Military Time


Hi!
It works good!!! Thanks for the help!
ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,214
#5: Jul 7 '08

re: Calculating Military Time


Quote:

Originally Posted by Serenityquinn15

Hi!
It works good!!! Thanks for the help!

You are quite welcome.
Reply


Similar Microsoft Access / VBA bytes