timer in form , how - complicated

john doe the 3rd

New Member
Joined
Mar 13, 2004
Messages
47
hey guys need someone with some advanced skills to answer this question:


is it possible to have a countdown timer , or a timer ( placed on form )which counts the elapsed time from the moment a user presses a start button ( placed on the form ) until that user presses the stop button (also placed on form )


many thanks.
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Hi JDIII,

The first thing you need to do is determine at what interval you want the timer to decrease. To do so, view the forms properties, and locate the 'Time Interval' property under the 'event' tab. This time interval is entered in 1/1000s of a second, so if you want something to happen every second (like your timer to countdown), set this to 1000.

Then add a button (cmdStart), a checkbox (chkOn) and a textbox (txtTime).

Now go the the VB editor and use this code for your form:

Code:
Private Sub cmdStart_Click()

    chkOn = True

End Sub

Private Sub Form_Load()

    chkOn = False
    txtTime = 0

End Sub

Private Sub Form_Timer()

    If chkOn = True Then
        If txtTime <= 0 Then chkOn = False
        txtTime = txtTime - 1
    End If

End Sub

Since the form timer event is firing every second, you need the checkbox to keep it from counting down until you click the start button.

Hope this helps,
 
Upvote 0
very nice, works but need it to be slightly modified, using your first code, the start button needs to be continually pressed for the counter to keep counting and display the time in the text box,

if i edit the code and change the last bit like this:

Code:
Private Sub Form_Timer()

    If Chkon = True Then
        If Txttime <= 0 Then Chkon = True
        Txttime = Txttime - 1
    End If

by setting the chkon value to true rather than false, the timer keeps counting after pressing the button once , but the button then fails to stop the counter if it is pressed again , the only way to stop the counter is by unticking the checkbox which makes the button useless for stopping :eek: any way you can get rid of this ugly checkbox and implement the same idea , into one button that starts and stops? , that would be great! :p .....

thanks.
 
Upvote 0
My original code should work. Because the timer event is always firing, you need someway to know whether your counting down what's in your textbox or not. So it checks the value of chkOn every second, if its checked, it counts down. If it reaches zero, chkOn is unchecked, and the though the code is still executing, everything is skipped because of the if statement.
This line:
If Txttime <= 0 Then Chkon = True
would have it counting past zero.

chkOn should not change value, unless you explicitly tell it to. If yours is unchecking, you must have additional code running or something.

Try this example:
www.theillumni.com/cort/counter.mdb

You'll notice you do not need to hit 'Start' to start counting down. The value if the checkox determines whether or not the textbox counts down. If you uncheck it, it stops counting, if you check it, it starts again. All the start button does is check the box. You could forego the whole check/button idea, and just use a toggle button, and test for the value of the button, True/False.

I provided an example of both methods.

Hope this helps,
 
Upvote 0
hmm.... something possibly wrong with my access?

even with ur example in the database you sent me the chkbox method i still need to keep pressing the button for the value to change , otherwise if i press it once it adds the value of "1" seconds but then stops , if i press it again it changes the value to "2" seconds.
the check box keeps unticking itself..

any idea why this is happening?
 
Upvote 0
The value in the box is from what number it starts counting down. So if you put 1, then then next number will be zero, and it will stop. Try putting something like 20 in the box, you will see it countdown to zero, and then stop. This would satisfy your request for a countdown timer.

To make a countup timer, just replace:
Txttime = Txttime - 1
with
Txttime = Txttime + 1
and you can lose the line:
If Txttime <= 0 Then Chkon = True
since it will always be counting up and will never reach zero.
 
Upvote 0

Forum statistics

Threads
1,221,831
Messages
6,162,242
Members
451,756
Latest member
tommyw

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top