Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

need Code for countdown clock 2

Status
Not open for further replies.

BadCook

Technical User
Sep 7, 2009
71
US
I'd like to put a countdown clock showing minuits and seconds on a form which would be activated by a command button. It would start say with 10 minuits and count down to zero when it would activate a msg box. Any help would be appreciated.
 


hi,

What code do you have so far, and where are you stuck?

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Skip, thank you for your interest.

I have a form with a timer a Label and a command button on it.
The code I'm using is preliminary and incomplete, this is my first attempt to use Time functions and I'm trying to get the hang of it.

Private Sub Command1_Click
Timer1.Enabled = False
Tim1 = Time$
Timer1.Enabled = True
Label1.Caption = TimeValue(Time$) - TimeValue(Tim1)
 
You are going to need a loop to constantly update the caption (eg. while time > 0, update caption) and you need to add a condition to trigger the message box when the count reaches 0.

If you are constantly updating the form, you may require 'DoEvents' to refresh the screen, although I know that several professionals in this forum will advise against it.

If at first you don't succeed, then sky diving wasn't meant for you!
 

Have a form with a timer, a Label, and a command button on it.
Code:
Option Explicit
Dim datStart As Date
Dim datStop As Date
Private Const intSeconds As Integer = 10

Private Sub Form_Load()

Label1.Caption = intSeconds
With Timer1
    .Enabled = False
    .Interval = 1000
End With

End Sub

Private Sub Command1_Click()

Command1.Enabled = False
datStart = Now
datStop = DateAdd("s", intSeconds, Now)
Timer1.Enabled = True

End Sub

Private Sub Timer1_Timer()

If datStart < datStop Then
    datStart = DateAdd("s", 1, datStart)
    Label1.Caption = DateDiff("s", datStart, datStop)
Else
    MsgBox "I am DONE."
    Timer1.Enabled = False
End If

End Sub

The code runs for a pre-set time of 10 seconds (intSeconds), but you can re-set it.

Have fun.

---- Andy
 
Nice solution Andy.

If at first you don't succeed, then sky diving wasn't meant for you!
 
ALl that date adding and subtracting seems a bit convoluted, though. Guiess it is an extract from a working program. Not really necessary for a simple integer countdown timer, which is what we've ended up with here. So could be shortened quite a lot to something like:
Code:
[blue]Option Explicit

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

Private Sub Command1_Click()
    Label1.Caption = 10 ' 10 secs
    Timer1.Enabled = True
End Sub

Private Sub Timer1_Timer()
    Label1.Caption = Label1.Caption - 1
    Timer1.Enabled = Label1.Caption <> "0"
End Sub[/blue]
But of course this doesn't quite address the OP's requirement for a display in minutes and seconds, which we can handle witrh a minor alteration, such as:
Code:
[blue]Option Explicit

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

Private Sub Command1_Click()
    Label1.Tag = 10 * 60 ' 10 mins
    Label1.Caption = Format(DateAdd("s", Label1.Tag, "00:00:00"), "nn:ss")
    Timer1.Enabled = True
End Sub

Private Sub Timer1_Timer()
    Label1.Tag = Label1.Tag - 1
    Label1.Caption = Format(DateAdd("s", Label1.Tag, "00:00:00"), "nn:ss")
    Timer1.Enabled = Label1.Tag <> "0"
End Sub[/blue]
 

I knew somebody will jump in and show a better way to do it. And I am not surprised it is you, strongm :)

Have fun.

---- Andy
 
Thanks guys for your interest and help.
Andy, your program worked like a charm
 
OOPS!
I meant strngm's code, but thank you all anyway.
 

count down to zero when it would [blue]activate a msg box[/blue]

Code:
Private Sub Timer1_Timer()
    Label1.Tag = Label1.Tag - 1
    Label1.Caption = Format(DateAdd("s", Label1.Tag, "00:00:00"), "nn:ss")
    Timer1.Enabled = Label1.Tag <> "0"[blue]
    If Not Timer1.Enabled Then
        MsgBox "I am Done."
    End If[/blue]
End Sub

Have fun.

---- Andy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top