How to get a Date Difference Value in milliseconds in VBA

mfaqueiroz

Registered User.
Local time
Yesterday, 18:39
Joined
Sep 30, 2015
Messages
125
Hei!
I've one table with dates in this format dd:mm:yyyy hh:mm:ss and in another column the milliseconds.

Do you have any suggestion how can I make the time difference in milliseconds?

I've write the following code, but isn't working for all cases

If Ms2 > Ms1 Then
Dif = DateDiff("s", Time1, time2)
Difms = ms2 - ms1
Dif= (Dif * 1000) + Difms

Else
Dif = DateDiff("s", Time1, time2)
Difms = (ms2 - ms1) + 1000
Dif = ((Dif - 1) * 1000) + Difms


Thank in advance for your help!:)
 
Last edited:
I easily found your solution here.
 
Hei,
Thank you IIKhoutz, I've read this solution, but I didn't get how can I integrate my Time1 and Time2 values in the suggested code, I'm sorry if this is really basic.....I'm newbie with access/vba.

Thank you for your help
 
If you check out https://support.microsoft.com/en-us/kb/210276 you will find out that the Date/Times are stored as a doubles basically representing days. The integer part is days and the decimal part is time as a fraction of a day. So one way of doing this is shown in the code below which you also find in the attached database if you want to test this.

I'm curious about your need for this. It seems usually to be subtracting days and needing precious down to the millisecond. Could you tell us where this data is coming from.?


Dim Time1 As Double
Dim Time2 As Double
Dim Time1MS As Variant
Dim Time2MS As Variant

Dim MS1 As Long
Dim MS2 As Long

Time1 = Me.Time1
Time2 = Me.Time2
MS1 = Me.MS1
MS2 = Me.MS2

Time1MS = Time1 * 24 * 3600 * 1000 ' Convert to millseconds
Time2MS = Time2 * 24 * 3600 * 1000 ' Convert to millseconds
Me.TimeDiff = (Time2MS + MS2) - (Time1MS + MS1)
 

Attachments

Last edited:
Hi, Back again. I just put your code in along side mine and I've getting the same results. So I'm not seeing where your code fails. In what case does your code produce the wrong result.
 

Attachments

Last edited:
I'm sorry for cross-post, I thought that wouldn't be any problem to ask the same question to get different points of view and share knowledge.
However, I understood that isn't "ethic", i will no do it again :)
thank you for advice and one more time sorry
 
Hi, Back again. I just put your code in along side mine and I've getting the same results. So I'm not seeing where your code fails. In what case does your code produce the wrong result.

Yes! mine is working! Was a problem with for cycles that weren't well done...
thank you for help and time! :)
 

Users who are viewing this thread

Back
Top Bottom