Discussion Locked

VB6 - getting 1/1000 of a second

0 helpful
8 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I am using VB6.
I was wondering if anyone knows an easy way of how to return the 1/1000ths of a second. I need this for a type of self-defined time stamp when a record is save to a MDB table, where the time stamp is ALWAYS unique. The problem is that I may be saving 100 records in a loop and 3-4 records are getting saved with-in 1 second so the date/time in the time-stamp/key field(text) may be the same for the 3 or 4 records - I am getting duplication errors.
I do not want to do this by having to place another timer object on my form.

I am not looking for any answer but for the best/simplest answer, and can only accept an answer that does what I need, or I am going to have to figure out another approach entirely.

Considering the constant speed improvement of PCs, I want to use 1/1000th of a second incase in a few years, for example, the saving process happens much faster where maybe many many more records get saved with-in one second.

I appreciate any help!

8 replies

by jay eckles

You just said yourself that your chosen method will be obsolete in a very short time - when performance improves so that multiple records are being stamped in the same milisecond. Why don't you do something like use whatever time stamp is convenient appended with a token that makes the stamp unique, like sequential numbers (1, 2, 3, ...) or a pseudo-random number drawn from a sufficiently large range, like 0-1000000?

In any case, if you want to do the whole millisecond stamp thing, the easiest way I can think of is perhaps code something in VC++ and use the old time_t struct which in part returns the current time as milliseconds since Jan 1970. Integrate it as a DLL, for example.

No matter what solution you use to get a millisecond stamp, how are you guaranteed the precision of that number? Even if you get a number in terms of milliseconds, how do you know the computer is counting by 5 ms or 100 ms? It still might not be a unique identifier.

Good luck.

Jay Eckles

by shanghai sam

Thanks for your response.
I didn't say that the "chosen method will be obsolete in a very short time" but that th?t considering the speed of PCs in the next few years, I want to be-able to save a time stamp in 1/1000ths of a second - meaning 1/10th or 1/100th is not enough but I will settle for 1/1000th - meaning 1000 records would have to be saved with-in the same second before problems start.

I want to solve this problem with the tools that I have - VB6 or API calls - I do not have C++ nor do I intend to buy it, nor do I want to rely on some Shareware DLL that may not work with the next OS update and I have no control over changing the code for a correction or possible work-a-round.

Precision: I am sure that if the the system clock can somehow be used to get this precision. Precision as far as the correct millisecond is not needed - If the same process is used every time then I would expect that the 2nd Save will always be X milliseconds - or ticks - higher than the previous Save - and the 3rd Save the same with respect to the 2nd, and so on.
The idea is to create a unique timestamp or key.

I see anyways that in a multi-user db the only way to guareentee a unique key is to add the user's or PC's name to the key. That solves that problem - now we are back at the first problem.

A simple counter just doesn't seem to impress me. A random number may also work but doesn't it use the timer as a starting point? If the save is with-in the same time, then the random number returned is the same.

I know that there must be some way with VB or API to get a 1/1000th PORTION of a second - this, with the use of the date/time and user's name would make it unique enough.

Thanks anyways.

by achp

First of all, do you need that your unique identifier were a timestamp?

Perhaps, you could do well with no extra programming by using two distinct fields: a timestamp field (if you really need to know when the record was inserted) and an auto-counter field to provide uniqueness. Believe me, Access's auto-counter fields work well in a multi-user environment.

If that's not the case, you could wish to use Win32 API's. Go to msdn.microsoft.com and search for functions like GetSystemTime or GetTickCount.

You could also look at Time overview with a full list of time-related functions and messages there.

by bbg

I do not like the idea of using an Auto-count field as this is not with-out problems when later changing to some other database.

But the use of the API call GetTicksCount along with the use of the Date/Time and a user name is good - There will be little or no chance of duplication.

by sl-campbell

the 2nd part of Answer #2 (using the API call to get the tick count) will work well when added to the current date and time and current user.

'Normally, the VB Timer function shows only a 1/100 of a decimal because it is automatically formated, but internally it is much more - which you can see when you do calculations with it -
- do a debug in the example below using sglTimerValue - Int(sglTimerValue).

Even though this number can be even smaller when using a double, it probably isn't needed. And the timer depends upon the OS - with each version the resolution of the system timer is different - I believe the value is around 50 milli-seconds - could be more now. So it will increment max in that amount of milli-seconds only.

You can get the same like this:

Public Function GetTickKey_Fs() As String

Static sglTimerValue As Double 'time of last key gotten (seconds past since 00:00:00)

'If the timer value since the last key was retrieved hasn't changed, then force a wait until it has
If Timer = sglTimerValue Then
Do Until Timer <> sglTimerValue
'wait a tick
Loop
End If

'Set variable to new timer value
sglTimerValue = Timer

Add the Ticks fraction to the current Date and Time
GetKey_Fs = Now() & Mid$(Round(sglTimerValue - Int(sglTimerValue), 5), 2, 5)

'Here you can add the user's name to the Key
'GetKey_Fs = GetKey_Fs & UsersName

End Function

by bbg

This works best with-out even having to use an API call - Seems to do the same as calling the GetTicksCount but is simple. Thanks!

This discussion is closed to new replies.