INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Label on UserForm to show time elapsing With Fractions Of Seconds

Label on UserForm to show time elapsing With Fractions Of Seconds

(OP)
I have a VBA UserForm in my Access 2003 database which displays the time elapsed. currently i have only coded it for hh mm ss (hours minutes seconds)

I need the label to display the time as it elapses in the format 00:00:00:00
(hours minutes seconds and fractions of seconds ie 100ths of seconds or milliseconds)

This is my code so far:

CODE -->

rivate Sub UpdateTimerLabel()
    Dim ss As Long
    Dim mm As Long
    Dim hh As Long
    Dim sglTimer As Single
    Const WAV_FILE As String = "C:\WINDOWS\MEDIA\tada.WAV"
    
    sglTimer = Timer
    Do
        ss = Int(Timer - sglTimer)
        If ss = 60 Then mm = mm + 1: ss = 0: sglTimer = Timer
        If mm = 60 Then hh = hh + 1:  mm = 0: sglTimer = Timer
        lblTimer.Caption = Format(hh, "00") & " Hrs : " & Format(mm, "00") & " mins : " & Format(ss, "00") & " Secs"
        DoEvents
    Loop Until bExit Or bScore Or bAbort
    If bScore Then
        PlaySoundAPI WAV_FILE, ByVal 0&, SND_FILENAME Or SND_LOOP Or SND_ASYNC
        If MsgBox("Congratulations " & sUserName & "  !!" & vbCrLf & vbCrLf & _
        "You scored in : " & Format(hh, "00") & " Hrs : " & Format(mm, "00") & " mins : " & Format(ss, "00") & " Secs" & vbCrLf & _
        "Do you want to save this score to your scores history  ?", vbQuestion + vbYesNo) = vbYes Then
            Call SaveTheScore(hh, mm, ss)
        End If
        PlaySoundAPI WAV_FILE, ByVal 0&, SND_FILENAME Or SND_PURGE
    End If
    lblTimer.Caption = ""
    Call EnableControls(True)
    Call DeletePreviousImages
    Set frameSourcePic.Picture = oPic
End Sub 

RE: Label on UserForm to show time elapsing With Fractions Of Seconds

Here's one way of doing it (I've simply cut your code down to the bare minimum for the sake of the example):

CODE

Private Sub UpdateTimerLabelExample
    Dim sglTimer As Double
    Dim elapsed As Double
    
    sglTimer = Timer
    Do
        elapsed = Timer - sglTimer
        lblTimer.Caption = TimeSerial(0, 0, elapsed) & ":" & Right("0" & Int(100 * (elapsed - Int(elapsed))), 2)
        ' Or
        ' lblTimer.Caption = TimeSerial(0, 0, elapsed) & ":" & Format(100 * (elapsed - Int(elapsed)), "00")
        ' Or to include milliseconds
        ' lblTimer.Caption = TimeSerial(0, 0, elapsed) & ":" & Format(10000 * (elapsed - Int(elapsed)), "00:00")
        DoEvents
    Loop Until bExit Or bScore Or bAbort
End Sub 

Note that there probably isn't much point going to milliseconds, since

a) default system timers in Windows only have a resolution of 15.625 milliseconds
b) it seems somewhat unlikely that the puzzle gane this is intended for relies on such tight reflexes that millisecond accuracy would be of any importance

Also note that in reality I'd be more disposed to be updating the timer on a timer event, rather than running in a tight loop as you do. This would involve a small amount of code refactoring, however, so I don't illustrate it here.

RE: Label on UserForm to show time elapsing With Fractions Of Seconds

Timer returns Single number, so you already have fractions of seconds, do not cut them with Int function.
Assuming that at sqlTimer counting starts, I would rather calculate passed time as PassedTime=Timer-sqlTimer, calculate hours, minutes and seconds (or format it as time), and add formatted fraction (multiply it by 100 or 1000 and format as full number).

combo

RE: Label on UserForm to show time elapsing With Fractions Of Seconds

(OP)
Thank you very much for your answers

I tried combo's suggestion and changed my code to
however i now get format 00:00:00 followed by seconds again as decimal ie 00.0000?
how do I format this correctly?

CODE -->

Private Sub UpdateTimerLabel()
    Dim ss As Long
    Dim mm As Long
    Dim hh As Long
    Dim sglTimer As Single
    Const WAV_FILE As String = "C:\WINDOWS\MEDIA\tada.WAV"
    Dim PassedTime
    sglTimer = Timer
    Do
    PassedTime = Timer - sglTimer
        ss = Int(Timer - sglTimer)
        If ss = 60 Then mm = mm + 1: ss = 0: sglTimer = Timer
        If mm = 60 Then hh = hh + 1:  mm = 0: sglTimer = Timer
        lblTimer.Caption = Format(hh, "00") & ":" & Format(mm, "00") & ":" & Format(ss, "00") & ":" & _
            PassedTime
        DoEvents
    Loop Until bExit Or bScore Or bAbort
    If bScore Then
        PlaySoundAPI WAV_FILE, ByVal 0&, SND_FILENAME Or SND_LOOP Or SND_ASYNC
        If MsgBox("Congratulations " & sUserName & "  !!" & vbCrLf & vbCrLf & _
        "You scored in : " & Format(hh, "00") & " Hrs : " & Format(mm, "00") & " mins : " & Format(ss, "00") & " Secs" & vbCrLf & _
        "Do you want to save this score to your scores history  ?", vbQuestion + vbYesNo) = vbYes Then
            Call SaveTheScore(hh, mm, ss)
        End If
        PlaySoundAPI WAV_FILE, ByVal 0&, SND_FILENAME Or SND_PURGE
    End If
    lblTimer.Caption = ""
    Call EnableControls(True)
    Call DeletePreviousImages
    Set frameSourcePic.Picture = oPic
End Sub 

RE: Label on UserForm to show time elapsing With Fractions Of Seconds

(OP)
Just tried strongm's suggestion and it produced a flicker free perfect solution.

Thank you both for your time and helpful advice.



Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close