Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Vb6 : Timer And Seconds Counting

Posted on 2004-10-02
14
Medium Priority
?
1,953 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
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!

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses

610 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