Solved

show time in m/s

Posted on 2001-06-18
23
147 Views
Last Modified: 2010-05-02
I have this code that tells me how many seconds have elapsed
Private BeginTime As Date
Private FinishTime As Date
Private ElapsedTime As Long
Private Sub Command1_Click()


'Get the beginning time
BeginTime = Now




End Sub

Private Sub Command2_Click()
'Get the time after you exit the loop
FinishTime = Now

'Figure how many seconds between them
ElapsedTime = DateDiff("s", BeginTime, FinishTime)

'Display like this or with debug
For I = 1 To 1000
Label1.Caption = "Elapsed time in seconds " & ElapsedTime
Next
End Sub
how can i get it show milliseconds
0
Comment
Question by:leachim
  • 7
  • 5
  • 4
  • +4
23 Comments
 
LVL 18

Expert Comment

by:deighton
ID: 6201580
ElapsedTime = (FinishTime - BeginTime) * 24# * 3600000#


note that the pc clock ticks in 15ths of a second though.  


also in your code you are calculating finish time before, and not after performing the loop.




0
 
LVL 4

Expert Comment

by:nutwiss
ID: 6201618
try this instead - the system keeps an internal ms tick count (the function gettickcount shows ms since the machine was powered on):



Option Explicit

Private Declare Function GetTickCount Lib "kernel32" () As Long

Dim start As Double

Private Sub Command1_Click()
start = GetTickCount
End Sub

Private Sub Command2_Click()
MsgBox GetTickCount - start
End Sub
0
 
LVL 49

Accepted Solution

by:
Ryan Chong earned 10 total points
ID: 6201623
Hi leachim, you can use the GetTickCount api as BeginTime and FinishTime:

Option Explicit
Private Declare Function GetTickCount Lib "kernel32" () As Long
Dim BeginTime As Long, FinishTime As Long


Private Sub Command2_Click()
    FinishTime = GetTickCount
    Command2.Caption = Format$((FinishTime - BeginTime) / 1000, "0.00")
End Sub

Private Sub Form_Load()
    BeginTime = GetTickCount
End Sub

'Hope this give you an idea.

0
 

Author Comment

by:leachim
ID: 6201625
deighton
can i just ask one thing more?
how can i format it so that it shows seconds and milliseconds
0
 
LVL 4

Expert Comment

by:nutwiss
ID: 6201628
ryancys's code shows how to format for seconds/ms
0
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 6201633
same idea with nutwiss but slow a little bit.
0
 
LVL 4

Expert Comment

by:nutwiss
ID: 6201647
and you'll have to excuse my appalling assignment of a long value to a double data-type ;)
0
 

Author Comment

by:leachim
ID: 6201655
Nutwiss
I am gratefull for your comments
I have accepted ryancys comment
but it has not registered yet
thanks again
0
 
LVL 4

Expert Comment

by:nutwiss
ID: 6201657
oh, go on, give me some points as well ;)

please? you know you want to!
0
 
LVL 18

Expert Comment

by:deighton
ID: 6201669
Dim ElapsedTime As Double

Dim sFormat As String

'whatever

ElapsedTime = finishtime - begintime

sFormat = Format(ElapsedTime, "#0.000")
MsgBox sFormat

0
 
LVL 6

Expert Comment

by:andyclap
ID: 6208038
deighton: that'll give you the number of days to 3dp!
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Expert Comment

by:ulischmidt
ID: 6211408


'Add the following code to your Module:
Public Declare Function timeGetTime Lib "winmm.dll" () As Long

'Add the following code to your form:
Private BeginTime As Long
Private FinishTime As Long
Private ElapsedTime As Long

Private Sub Command1_Click()
  'Get the beginning time
  BeginTime = timeGetTime
End Sub

Private Sub Command2_Click()
  'Get the time
  FinishTime = timeGetTime

  'Figure how many milliseconds between them
  ElapsedTime = FinishTime - BeginTime

  'Display like this or with debug
  Label1.Caption = "Elapsed time in ms " & ElapsedTime

End Sub
0
 
LVL 4

Expert Comment

by:nutwiss
ID: 6211624
ulischmidt ,

unfortunately, I believe this requires you have sound hardware installed for a multimedia timer to be present
0
 
LVL 4

Expert Comment

by:nutwiss
ID: 6211625
ps he's already accepted ryancys' answer.....
0
 
LVL 6

Expert Comment

by:andyclap
ID: 6211642
Most people nowadays do, so it's valid to use it, however you really need to check that you're capable first, and degrade to the old inaccruate method if not.
0
 

Author Comment

by:leachim
ID: 6211658
hes already accepted ryancys' answer three times but to no avail
0
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 6212660
Hi leachim, is it my comment solve your problem?
0
 

Expert Comment

by:ulischmidt
ID: 6213207
Hi nutwis
Of course u have always to check the capabilities for all timer functions, especially for the multimedia timer functions, which provide higher precision, you can do this with the timeGetDevCaps function.
declarations:

Type TIMECAPS
  wPeriodMin as Long 'Minimum supported resolution
  wPeriodMax as Long 'Maximum supported resolution
End Type
public declare function timeGetDevCaps lib "winmm.dll" alias "timeGetDevCaps"(lpTimeCaps as TIMECAPS, ByVal uSize as Long)

for this u need no Sound-hardware!
0
 
LVL 4

Expert Comment

by:nutwiss
ID: 6213434
true, true.... ;)
0
 

Author Comment

by:leachim
ID: 6214672
ryancys
yes i have accepted your comment as answer three times but it has not acknowledged so
0
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 6216608
Hi leachim, sorry about that due to my poor english. so what can i help you. : )
0
 

Author Comment

by:leachim
ID: 6224292
ryancys
you have helped already
thanks
0
 

Expert Comment

by:costello
ID: 6400341
Rejecting proposed answer.

Trying to force accept ryancys' comment.

costello
Community Support Moderator @ Experts-Exchange
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Vb.net 2008 2 56
VBA filters 2 51
Access query that references subform 5 43
VB 6 error 5 in windows 10 but not in XP 7 46
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now