Dim Hours As Integer
Dim Minutes As Integer
Dim Seconds As Integer
Dim Time As Date
Private Sub Mydisplay()
'This code is common to all three text boxes so I
'put it in it's own sub.
 
'Extract the numbers from the text boxes by using
'the Val() statement.
    Hours = Val(Text1.Text)
    Minutes = Val(Text2.Text)
    Seconds = Val(Text3.Text)
'Convert variables to time format
    Time = TimeSerial(Hours, Minutes, Seconds)
'Display the converted time variable in label 1
    Label1.Caption = Format$(Time, "hh") & ":" & Format$(Time, "nn") & ":" & Format$(Time, "ss")
End Sub
 
Private Sub Command1_Click()
'Start button - turn on timer and disable reset
'button
    Timer1.Enabled = True
    Command3.Enabled = False
End Sub
 
Private Sub Command2_Click()
'Pause button - temporarily stop timer and enable
'reset button. You can restart countdown by clicking
'the start button. Or reset the time by clicking
'the reset button.
    Timer1.Enabled = False
    Command3.Enabled = True
End Sub
 
Private Sub Command3_Click()
'Reset button - reset all varibles and text boxes
'to nothting.
    Hours = 0
    Minutes = 0
    Seconds = 0
    Time = 0
    Text1.Text = " "
    Text2.Text = " "
    Text3.Text = " "
    Text1.SetFocus 'put curser in the hour text box
End Sub
 
Private Sub Command4_Click()
'Exit button - end program and clear varibles
    End
End Sub
 
Private Sub Form_Load()
'Center form on screen.
Form1.Top = (Screen.Height - Form1.Height) / 2
Form1.Left = (Screen.Width - Form1.Width) / 2
'Set timer interval and varibles
    Timer1.Interval = 1000
    Hours = 0
    Minutes = 0
    Seconds = 0
    Time = 0
End Sub
 
Private Sub Text1_Change()
'Call Mydisplay sub to display text box data
    Mydisplay
End Sub
 
Private Sub Text2_Change()
'Call Mydisplay sub to display text box data
    Mydisplay
End Sub
 
Private Sub Text3_Change()
'Call Mydisplay sub to display text box data
    Mydisplay
End Sub
 
Private Sub Timer1_Timer()
    'Count down loop
    Timer1.Enabled = False
    If (Format$(Time, "hh") & ":" & Format$(Time, "nn") & ":" & Format$(Time, "ss")) <> "00:00:00" Then 'Counter to continue loop until 0
        Time = DateAdd("s", -1, Time)
        Label1.Visible = False
        Label1.Caption = Format$(Time, "hh") & ":" & Format$(Time, "nn") & ":" & Format$(Time, "ss")
        Label1.Visible = True
        Timer1.Enabled = True
    Else
        'Turn off timer, set off alarm, and enable reset.
        Timer1.Enabled = False
        Beep
        Beep
        Command3.Enabled = True
    End If
End Sub

Using the code above im able to make it work but you have to input the hh/mm/ss manually instead i want to already have a set time placed in the code so as soon as i hit start the countdown begins on the label i would have right under the command button. For example one of the times ill be using will be 2hrs. so when i click the command button i want the numbers to show up on the label and automatically begin it's 2hr countdown.

Why not add a command button that has code something like.

Hours = 2
Minutes = 0
Seconds = 0

Time = TimeSerial(Hours, Minutes, Seconds)

Text1.Text = "2"
Text2.Text = "0"
Text3.Text = "0"
Timer1.enabled = true

also at line 84 why not say

if label1.caption <> "00:00:00" then

Why not add a command button that has code something like.

Hours = 2
Minutes = 0
Seconds = 0

Time = TimeSerial(Hours, Minutes, Seconds)

Text1.Text = "2"
Text2.Text = "0"
Text3.Text = "0"
Timer1.enabled = true

also at line 84 why not say

if label1.caption <> "00:00:00" then

Ya the entire point is that i dont want any text box's so i'll change it around a bit.

Just delete them, contol the whole thing from the 3 integer variables, or if you are after a quick and dirty solution simply change their .visible property to False. That way you don't need to worry about stray references to them causing problems.

So i am now down to just thise code

Dim Hours As Integer
Dim Minutes As Integer
Dim Seconds As Integer
Private Sub Command1_Click()
Timer1.Enabled = True
End Sub
Private Sub Command2_Click()
Unload Form1
End Sub
Private Sub Form_Load()
'Center form on screen.
Form2.Top = (Screen.Height - Form1.Height) / 2
Form2.Left = (Screen.Width - Form1.Width) / 2
'Set timer interval and varibles
Timer1.Interval = 1000
Hours = 0
Minutes = 0
Seconds = 0
Label1.Caption = "02:00:00"
End Sub
Private Sub Timer1_Timer()
'Count down loop.
If Label1.Caption <> "00:00:00" Then
Label1.Caption = Label1.Caption - 1
 Timer1.Enabled = False
 Label1.Visible = False
      Label1.Visible = True
    Timer1.Enabled = True
    Else
    'Turn off timer, set off alarm, and enable reset.
    Timer1.Enabled = False
    Beep
    Beep
    End If
End Sub

And all that is happenning to me is that 02:00:00 will appear once i start the program but as soon as i hit the start button to start it's countdown nothing happens. All i want is the time in the code so when you hit start the countdown automatically begins. i've seen other codes that do what i want but they count down in seconds only not hh/mm/ss which is what i need.

Have a look at the attachment, something I used quite a while back. It will give you all the basics of a count down timer.:)

Have a look at the attachment, something I used quite a while back. It will give you all the basics of a count down timer.:)

This was the code that i was originally using but it still requires text box. im saying i want this same code or close to it but without any text box and with the actual time already in the code so when i click start and click the command button. A countdown starts Automatically.

Quick one, do you want to count down or start at a specific time and the count onwards from there?

Quick one, do you want to count down or start at a specific time and the count onwards from there?

For example i want to be able to click the command button making 02:00:00 Appear on the label and for it to start to countdown.

No need of a command button. Just add the time at the form load event. The timer and the function takes care of the rest.:)

(Reputation Time!!!);)

Option Explicit

Private Function CountDown(lbl As Label)

On Error Resume Next

Dim lHrs As Long
Dim lMinutes As Long
Dim lSeconds As Long

'Break the time format into parts...
lHrs = DatePart("h", lbl.Caption)
lMinutes = DatePart("n", lbl.Caption)
lSeconds = DatePart("s", lbl.Caption)

'All obvious...
If lMinutes = 0 Then
    lMinutes = 60
    lHrs = lHrs - 1
End If

If lSeconds = 0 Then
    lMinutes = lMinutes - 1
    lSeconds = 60
End If

'Deduct a second...
lSeconds = lSeconds - 1

If lHrs < 10 Then lHrs = "0" & lHrs
If lMinutes < 10 Then lMinutes = "0" & lMinutes
If lSeconds < 10 Then lSeconds = "0" & lSeconds

'Add the new time to the label
lbl.Caption = lHrs & ":" & lMinutes & ":" & lSeconds
End Function

Private Sub Form_Load()

Label1.Caption = Format("02:00:00", "hh:mm:ss")

'Change the time to whatever you require, in this case 2 hours...
End Sub

Private Sub Timer1_Timer()

'Call the function to start the countdown...
Call CountDown(Label1)
End Sub

As much as you say i don't need one i honestly really do because i'm going to be having about 16 command boxes and the code will be going into each and everyone of them.

Will they all start at the same time? If so then there is a quicker way around. If not, command buttons is the way then.

Apart from that, Is the code what you were looking for? If so, please mark the thread as solved, found at the bottom of this page, thanks.

No each Command Button will have a different set time and i will try this piece of code in a bit when im more awake.

Cool. Under the commandbutton just call the function with the label that will show the time for that button.

Have some coffee.;) Is it morning or night by you that you still need to wake up?:)

It was around 8am for me but i went to bed late. So i tested out the code and im not to sure if messed up somewhere along the lines but i get as far as the time is alerady appeared on the label. The time only decreases by one everytime i hit the command button. so if i had the time set for 30 seconds i would have to click the command button 30 times, instead of once and allowing it to countdown on its own.

Nope. Add the call event under a timer with enable property to false. Call the function under the timer. Under the command button, set timer enable to true.

Alot of that code and just what you used wasn't doing anything but giving me errors and i wasn't sure why so i took some things out and change the variables around this is my code right now. i know its not what you showed me but ya. could you tell me exactly what to fix?

Dim lHrs As Integer
Dim lMinutes As Integer
Dim lSeconds As Integer

Private Sub Command1_Click()
lHrs = DatePart("h", label1.Caption)
lMinutes = DatePart("n", label1.Caption)
lSeconds = DatePart("s", label1.Caption)


If lMinutes = 0 Then
   lMinutes = 60
   lHrs = lHrs - 1
End If

If lSeconds = 0 Then
   lMinutes = lMinutes - 1
   lSeconds = 60
End If
lSeconds = lSeconds - 1

If lHrs < 10 Then lHrs = "0" & lHrs
If lMinutes < 10 Then lMinutes = "0" & lMinutes
If lSeconds < 10 Then lSeconds = "0" & lSeconds

'Add the new time to the label
label1.Caption = lHrs & ":" & lMinutes & ":" & lSeconds
End Sub

Private Sub Form_Load()
label1.Caption = Format("01:00:00", "hh:mm:ss")
End Sub

Private Sub Timer1_Timer()
Call CountDown(label1)
End Sub

Correct. Just paste my code as it was and only add a command button with the following code -

Timer1.Enabled = True

That is all you need for the code to work. Do not change anything, you will get confused, especially with thirty command buttons.;)

Correct. Just paste my code as it was and only add a command button with the following code -

Timer1.Enabled = True

That is all you need for the code to work. Do not change anything, you will get confused, especially with thirty command buttons.;)

Alright disregard my previous post then. ill test this out.

I have altered the code and tested it up to 2 hours. It is all working fine now...:)

The NEW code -

Option Explicit
 
Private Function CountDown(lbl As Label, tmr As Timer)
 
Dim lHrs As Long, lMinutes As Long, lSeconds As Long
Dim iHours As Integer, iMinutes As Integer, iSeconds As Integer
Dim Time As Date

lHrs = DatePart("h", lbl.Caption)
lMinutes = DatePart("n", lbl.Caption)
lSeconds = DatePart("s", lbl.Caption)

iHours = lHrs
iMinutes = lMinutes
iSeconds = lSeconds

'Convert variables to time format
Time = TimeSerial(iHours, iMinutes, iSeconds)

'Display the converted time variable in label 1
lblTime.Caption = Format$(Time, "hh") & ":" & Format$(Time, "nn") & ":" & Format$(Time, "ss")
    
If (Format$(Time, "hh") & ":" & Format$(Time, "nn") & ":" & Format$(Time, "ss")) <> "00:00:00" Then 'Counter to continue loop until 0
    Time = DateAdd("s", -1, Time)
    lblTime.Visible = False
    lblTime.Caption = Format$(Time, "hh") & ":" & Format$(Time, "nn") & ":" & Format$(Time, "ss")
    lblTime.Visible = True
    tmr.Enabled = True
        Else
    'Turn off timer, set off alarm, and enable reset.
    tmr.Enabled = False
End If
End Function

Private Sub Command1_Click()

'Start button - turn on timer
Timer1.Interval = 1000 '1 Second intervals...
Timer1.Enabled = True
End Sub

Private Sub Form_Load()

lblTime = "02:01:03"
End Sub

Private Sub Timer1_Timer()

Call CountDown(lblTime, Timer1)
End Sub

this code is much better it actually counts down now and works without a problem thankyou very much. :)

It was a pleasure.:)

Happy coding.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.