Solved

Vb6 : Timer And Seconds Counting

Posted on 2004-10-02
14
1,950 Views
Last Modified: 2012-06-21
Id like to start a timer to count in seconds when i click a command button


what would the code be ??? vb6:

output to label or text box, or better.

thankyou
0
Comment
Question by:Jimmyx1000
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
14 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12208133
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 12208201
Create a new project and add a timer, label and button:

Option Explicit

Private startTime As Date

Private Sub Form_Load()
    Timer1.Interval = 333 ' update counter 3 times a second (adjust to your liking)
    Timer1.Enabled = False
End Sub

Private Sub Command1_Click()
    startTime = Now
    Timer1.Enabled = True
End Sub

Private Sub Timer1_Timer()
    Label1.Caption = DateDiff("s", startTime, Now)
End Sub
0
 
LVL 2

Expert Comment

by:gimmeadrink
ID: 12209894
Im sure if you search around, you will find many ppl saying how inaccurate the timer is.

If dead accuracy is not that much of an issue for you, then the timer as suggested above is perfect.

If accuracy is important, then i highly reccomend ccrptimer. It was developed by  Karl E. Peterson who is one of microsofts Most Vaulable Professionals. I only using the library now, its fantastic.

The other great thing about it... its not a control, its a library, meaning you dont need one on each form u need a timer, you dont even need a form at all.

Its available here: http://ccrp.mvps.org/index.html?controls/ccrptimer6.htm 

Its very simple to use and has a sample project.

If you want any help using it, just let me know.
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 1

Accepted Solution

by:
InVisal earned 500 total points
ID: 12209958
Dim a As Integer
Private Sub Form_Load()
    Timer1.Interval = 1000
    Timer1.Enabled = False
End Sub

Private Sub Command1_Click()
    a = 0
    Timer1.Enabled = True
End Sub

Private Sub Timer1_Timer()
    a = a + 1
    Label1.Caption = a
End Sub
0
 

Expert Comment

by:Camden04
ID: 12209990
Sub DelayPre(preset)
 'delay in seconds.000

    Dim start#
    Dim Check#
    start = Timer
    Do Until Check >= start + preset
        DoEvents
        Check = Timer
    Loop
End Sub

Sub Command1_Click()
Dim x as Long
x = 0
   Do
      DelayPre(1)
      x = x + 1      
      Label1.Caption = x
   DoEvents
   Loop
End Sub
     
0
 

Author Comment

by:Jimmyx1000
ID: 12210544
InVisal

I need to put the

Doevents command

in your code where is
the correct place to put this in.

Dim a As Integer
Private Sub Form_Load()
    Timer1.Interval = 1000
    Timer1.Enabled = False
End Sub

Private Sub Command1_Click()
    a = 0
    Timer1.Enabled = True
End Sub

Private Sub Timer1_Timer()
    a = a + 1
    Label1.Caption = a
End Sub
0
 
LVL 1

Expert Comment

by:InVisal
ID: 12210816
no need do events
it work fine
0
 

Author Comment

by:Jimmyx1000
ID: 12210884
how Can i get this function to display as h m s  : hours minutes seconds

thanks
0
 
LVL 1

Expert Comment

by:InVisal
ID: 12210942
Dim s As Integer
Dim m As Integer
Dim h As Integer

Private Sub Form_Load()
    Timer1.Interval = 1000
    Timer1.Enabled = False
End Sub

Private Sub Command1_Click()
    s = 0
    m = 0
    h = 0
    Timer1.Enabled = True
End Sub

Private Sub Timer1_Timer()
    s = s + 1
   
    If s = 60 Then
        m = m + 1
        s = 0
    End If
   
    If m = 60 Then
        m = 0
        h = h + 1
    End If
   
    Label1.Caption = h & ":" & m & ":" & s
End Sub
0
 
LVL 1

Expert Comment

by:InVisal
ID: 12210966
or this code

Dim a As Integer

Private Sub Form_Load()
    Timer1.Interval = 1000
    Timer1.Enabled = False
End Sub

Private Sub Command1_Click()
    a = 0
    Timer1.Enabled = True
End Sub

Private Sub Timer1_Timer()
   
    Dim m, s, h As Integer

    a = a + 1
    m = Int(a / 60)
    s = a - (m * 60)
    h = Int(m / 60)
    m = m - (h * 60)
   
    Label1.Caption = h & ":" & m & ":" & s
   
End Sub
0
 
LVL 1

Expert Comment

by:VBtorment
ID: 12211029
or this

dim StartTime


Private Sub Form_Load()
    Timer1.Interval = 500
    Timer1.Enabled = False
End Sub

Private Sub Command1_Click()
    StartTime = now
    Timer1.Enabled = True
End Sub


private sub Timer1_Timer()
    Label1.caption= Format$(Now - starttime, "HH:MM:SS")
End Sub

just reset StartTimer to now to start again
0
 

Author Comment

by:Jimmyx1000
ID: 12211077
InVisal

Your code display a result of

0:0:0 format

can you do a

00:00:00 format

apart from this that should be it

0
 
LVL 1

Expert Comment

by:InVisal
ID: 12211100
yeah

Label1.Caption = Right("0" & h, 2) & ":" & Right("0" & m, 2) & ":" & Right("0" & s, 2)
0
 

Author Comment

by:Jimmyx1000
ID: 12211144
Ok thankyou expert

Well Done
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

707 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