PDA

View Full Version : Timer & Decimal Places



Dowsey1977
03-10-2006, 08:59 AM
Hi,I am using timer in a form I have to calculate how long a form is open for, so timer when the form is opened, and timer when a button is pressed. I then take the 2nd timer from the first to get the difference. However, it currently shows a few decimal places after the seconds, and ideally I want to get this down so it purely shows whole seconds. Any ideas if this is possible??Also, it would be good to be able to show minutes and seconds, if the number goes above 60, or minutes just as a 0 if this is not possible.Any help appreciated.

Killian
03-10-2006, 09:39 AM
I suppose it depends on the format you're dealing with.
Here's an approach using the Date formatDim datStart As Date

Private Sub UserForm_Initialize()
datStart = Now()
End Sub

Private Sub cmdStop_Click()

Dim datEnd As Date
Dim datDiff As Long
Dim strResult As String

datEnd = Now()
datDiff = DateDiff("s", datStart, datEnd)
If datDiff > 60 Then
strResult = datDiff \ 60 & "m " & datDiff Mod 60 & "s"
Else
strResult = datDiff & "s"
End If

MsgBox strResult

End Sub

Dowsey1977
03-10-2006, 11:55 AM
Excellent...thanks!

smc2911
03-10-2006, 07:54 PM
Or use Format(datDiff, "N:mm"), where N specifies minutes without a leading zero.

Sean.

Bob Phillips
03-11-2006, 03:14 AM
I would use Timer


Dim nTime As Double

Private Sub UserForm_Initialize()
nTime = Timer
End Sub

Private Sub cmdStop_Click()


MsgBox Format(Timer - nTime, "0.0 secs")

End Sub