?
Solved

Vb6 : Timer And Seconds Counting

Posted on 2004-10-02
14
Medium Priority
?
1,951 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 1

Accepted Solution

by:
InVisal earned 2000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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 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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month14 days, 22 hours left to enroll

771 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