469,270 Members | 1,117 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,270 developers. It's quick & easy.

How to add milliseconds to Now()?

547 512MB
I need to add milliseconds to this finishtime of race

Expand|Select|Wrap|Line Numbers
  1. [Forms]![RaceSetupttF]![RaceTimingTTSF1]![RaceFinishTime] = Format(Now(), "General Date")
Start code for race also in milliseconds
Expand|Select|Wrap|Line Numbers
  1. [Forms]![RaceSetupttF]![Rt_StartTTSF]![startTT] = Format(Now(), "General Date")
I will then subtract it from the start time and get a result in milliseconds
Expand|Select|Wrap|Line Numbers
  1. RacingTime: Format([Racefinishtime]-[startTT],"hh:nn:ss")
Any suggestions on how this can be accomplished?
Jan 27 '11 #1
16 35742
TheSmileyCoder
2,321 Expert Mod 2GB
Access Now() will only record as accurate as down to 1 second. So you can't use that. I also dont think (but dont know for sure) that you can store time in date/time field more accurately then down to the second. And you will still need to custom format it when you want to display it.

Now this is something I use to time my procedures to see how long they take, and how different approaches can shorten the run time.

Place this code in a CLASS module called StopWatch. (I did not write the original code, though I did modify it somewhat)
Expand|Select|Wrap|Line Numbers
  1. Private mlngStart As Long
  2. Private Declare Function GetTickCount Lib "kernel32" () As Long
  3.  
  4. Public Sub StartTimer()
  5.     mlngStart = GetTickCount
  6. End Sub
  7.  
  8. Public Function EndTimer() As String
  9.     Dim EndTime As Long
  10.     EndTime = (GetTickCount - mlngStart)
  11.     Dim s As Integer
  12.     s = EndTime / 1000
  13.     Dim ms As Integer
  14.     ms = EndTime Mod 1000
  15.     If s > 1 Then
  16.         EndTimer = s & "s, " & ms & "ms"
  17.     Else
  18.         EndTimer = ms & "ms"
  19.     End If
  20. End Function
When I use this timer it looks like this:
Expand|Select|Wrap|Line Numbers
  1. Private sub MyFunction()
  2.   'Start stopwatch
  3.   Dim mySW as stopwatch
  4.   mySW.StartTimer
  5.  
  6.   'Rest of my code goes here
  7.  
  8.   'Reached the end of code, printout how long it took to run it
  9.   Debug.Print "MyFunction ran in:" & mySW.EndTimer
  10. End Sub
Jan 27 '11 #2
neelsfer
547 512MB
Thx for the advice so far. I am struggling with modules and to link to them from a button.
i am attaching part of the application ( deleted parts of it to make it smaller)
How do i add these milliseconds functions to the "Start Rider" and "Add time" buttons? Both buttons use Now() currently. Will it be different to deduct the finish and start times now? Please help.
Attached Files
File Type: zip Racetiming v5.1 -milisec 2003.zip (633.6 KB, 554 views)
Jan 28 '11 #4
ADezii
8,800 Expert 8TB
The way that I see things is that what you are requesting would be difficult to implement, for the reasons given by TheSmileyOne. You can display the Current Time as 16:48:01:0913, but since this is not a true Date Value, you would not be able to perform mathematical calculations on in without the addition of some innovative Code.
Jan 29 '11 #5
neelsfer
547 512MB
The only calculation i am going to have for this milliseconds on one of my forms, is finishtime minus starttime (with mulliseconds), No additions required etc. Is that possible?
Its for a specific type of racing called timetrial where they race individually from point A to B and back to A. You may have 2 persons with the same racingtime and that's where the milliseconds come in
Jan 30 '11 #6
Stewart Ross
2,545 Expert Mod 2GB
I've adapted the code originally supplied by Mary to return the system time in milliseconds as a long int value. This can be used as in the test function to assign times then subtract them afterwards to get the true difference in milliseconds.

I hope this helps.

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Private Type SYSTEMTIME
  5.   wYear As Integer
  6.   wMonth As Integer
  7.   wDayOfWeek As Integer
  8.   wDay As Integer
  9.   wHour As Integer
  10.   wMinute As Integer
  11.   wSecond As Integer
  12.   wMilliseconds As Integer
  13. End Type
  14.  
  15.  
  16. Private Declare Sub GetSystemTime Lib "kernel32" _
  17. (lpSystemTime As SYSTEMTIME)
  18.  
  19. Public Function TimeToMillisecond() As Long
  20.     Dim tSystem As SYSTEMTIME
  21.     Dim sRet As Long
  22.     On Error Resume Next
  23.     GetSystemTime tSystem
  24.     With tSystem
  25.         sRet = (CLng(.wHour) * 60 * 60 * 1000) + (CLng(.wMinute) * 60 * 1000) + CLng(.wSecond) * 1000 + .wMilliseconds
  26.     End With
  27.     TimeToMillisecond = sRet
  28. End Function
  29.  
  30.  
  31.  
  32. Public Function TimeTest()
  33.     Dim lngV1 As Long, lngV2 As Long, lngCounter As Long
  34.     lngV1 = TimeToMillisecond
  35.     For lngCounter = 1 To 1000000000
  36.     Next
  37.     lngV2 = TimeToMillisecond
  38.     Debug.Print "First Time: " & lngV1, "Second Time: " & lngV2, "Diff (ms): " & lngV2 - lngV1
  39. End Function
  40.  
Example of test run:

? TimeTest
First Time: 37081308 Second Time: 37088349 Diff (ms): 7041

-Stewart
Jan 30 '11 #7
Stewart Ross
2,545 Expert Mod 2GB
Oops. I forgot to provide a way to show the system time in ms in its normal form:

Expand|Select|Wrap|Line Numbers
  1. Public Function ShowSysTime(ByVal Tms As Long) As String
  2.     Const MsInHour = 3600000
  3.     Const MsinMin = 60000
  4.     Const MsInSec = 1000
  5.     Dim lngHrs As Long
  6.     Dim lngMins As Long
  7.     Dim lngSecs As Long
  8.     Dim lngMs As Long
  9.     lngHrs = Tms \ MsInHour
  10.     Tms = Tms - (lngHrs * MsInHour)
  11.     lngMins = Tms \ MsinMin
  12.     Tms = Tms - (lngMins * MsinMin)
  13.     lngSecs = Tms \ MsInSec
  14.     Tms = Tms - (lngSecs * MsInSec)
  15.     lngMs = Tms
  16.     ShowSysTime = Format(lngHrs, "00") & ":" & Format(lngMins, "00") & ":" & Format(lngSecs, "00") & ":" & Format(lngMs, "000")
  17. End Function
Test run:
? ShowSysTime(TimeToMillisecond)
10:38:19:478

-Stewart

PS Please note that neither Mary's original code nor my adapted versions above handle transitions from one day to the next where the times extend past midnight (e.g. for 23:30:00:567 to 00:01:30:123). The SystemTime type does contain a .wDay component that could be used in extending the code provided for that purpose if necessary.
Jan 30 '11 #8
neelsfer
547 512MB
thx for the trouble. How do i incorporate it into a form and link it to a button. I am novice but learning fast.
Jan 30 '11 #9
ADezii
8,800 Expert 8TB
The following Code takes a radically different approach, and incorporates all logic in a single Function Procedure. It accepts 2, precisely formatted String Arguments representing Time Values in the Format of:
Expand|Select|Wrap|Line Numbers
  1. hh:nn:ss:llll
  2. h = Hours
  3. n = Minutes
  4. s = seconds
  5. l = Milliseconds
  6.  
  7. Examples:
  8. 12:43:13:0854
  9. 23:02:58:0012
  10.  
The Function will then return the Time Difference in Milliseconds. For the sake of simplicity and brevity, it performs minimal Validations on the Inputs and does not take into consideration the Midnight barrier. I don't really think this would apply to races anyway. I have not spent that much time on the Logic, so it may need a little work.

P.S. - I'll post the Code, but feel free to Download the Attachment, play with it, and see what you think. Any questions, feel free to ask.
Expand|Select|Wrap|Line Numbers
  1. Public Function fCalcTimeDifference(strStart As String, strFinish As String) As Long
  2. Dim dteStart As Date
  3. Dim dteFinish As Date
  4. Dim lngMilliDiff As Long
  5. Dim lngStartMilliSecs As Long
  6. Dim lngFinishMilliSecs As Long
  7.  
  8. 'Extract Real Time Values from strStart minus Milliseconds
  9. dteStart = TimeValue(Left$(strStart, 8))
  10. dteFinish = TimeValue(Left$(strFinish, 8))
  11. lngStartMilliSecs = Val(Right$(strStart, 4))
  12. lngFinishMilliSecs = Val(Right$(strFinish, 4))
  13.  
  14. 'If the Base Times are equal, and the only difference is in Milliseconds, as in:
  15. '11:30:10:0099 - 11:30:10:0345
  16. If (dteStart = dteFinish) And (lngFinishMilliSecs > lngStartMilliSecs) Then
  17.   fCalcTimeDifference = (lngFinishMilliSecs - lngStartMilliSecs)
  18.     'Debug.Print dteStart & " ==> " & dteFinish
  19. Else        'Start Time <> Finish Time
  20.   'Check the Millisecond Component of the time to see if Finish Millisecond >
  21.   'Start Millisecond, if this is not true, then we need to 'borrow' 1 second
  22.   '(1,000 Millisecond) from the Finish
  23.   If lngFinishMilliSecs > lngStartMilliSecs Then      'We're good
  24.     'Convert Time Differential in Milliseconds
  25.     lngMilliDiff = (DateDiff("s", dteStart, dteFinish) * 1000) + _
  26.                    (lngFinishMilliSecs - lngStartMilliSecs)
  27.       fCalcTimeDifference = lngMilliDiff
  28.   Else
  29.     'Need to 'borrow' 1 second from Finish Time and add it to Milliseconds Component
  30.     dteFinish = DateAdd("s", -1, dteFinish)       'Borrowed a second, will add to
  31.                                                   'Millisecond Component
  32.       lngMilliDiff = (DateDiff("s", dteStart, dteFinish) * 1000) + _
  33.                      ((lngFinishMilliSecs + 1000) - lngStartMilliSecs)
  34.       fCalcTimeDifference = lngMilliDiff
  35.   End If
  36. End If
  37. End Function
Attached Files
File Type: zip Milliseconds.zip (17.4 KB, 520 views)
Jan 30 '11 #10
neelsfer
547 512MB
thx to all for the help
Jan 31 '11 #11
neelsfer
547 512MB
Would you please add start and finish buttons on this form ? i would like to see how you get the times into a table.
Thx
Jan 31 '11 #12
ADezii
8,800 Expert 8TB
Kindly clarify.
Jan 31 '11 #13
neelsfer
547 512MB
Sorry man. A (start) button on a form to add the "Now()+milliseconds" in one field of a table. i can then add a button for the finishtime and then there is the calculation in a query to calculate the difference between start and finish time
Jan 31 '11 #14
ADezii
8,800 Expert 8TB
  1. Here is an Example of how you can Format the Current Time + Milliseconds into the required String along with the subsequent Output:
    Expand|Select|Wrap|Line Numbers
    1. Dim intMilliseconds As Long
    2.  
    3. intMilliseconds = 672
    4.  
    5. Debug.Print Format$(Time, Format$("hh", "00")) & ":" & Format$(Time, Format$("nn", "00")) & _
    6.             ":" & Format$(Time, Format$("ss", "00")) & ":" & Format$(intMilliseconds, "0000")
    Expand|Select|Wrap|Line Numbers
    1. 19:28:09:0672
  2. Generate the Finish Time in the same manner.
  3. Pass both Start and Finish Times to the fCalcTimeDifference() Function in a Calculated Field in a Query to retrieve the Results in Milliseconds.
Feb 1 '11 #15
Neelsfer, don't know if you still do this. I'm trying to write the same thing you did back in 2011. Just found this thread. Did you ever get this working? Can you share the code or even the app? I'm doing this for my cycling club. We hold a TT every year for a bunch of casual riders.. Thanks in advance.
Mar 9 '17 #16
I use a spread sheet now that does seeding, assigns race number and generates the start roster. It allows for time capture at the finish and calculates the splits for each rider. I'm looking at modifying the sheet to accept RFID input automatically but would prefer to rewrite this in an access app.
Mar 9 '17 #17

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by Richard Visser | last post: by
9 posts views Thread by gregg | last post: by
21 posts views Thread by Willie jan | last post: by
24 posts views Thread by Joe, G.I. | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.