Form Timer

N

Nick T

Hi,
Been trying to solve this one for days now:

I have a text box (called Timer1) on my form which i want to use as a
countdown timer.
The defult value for 'Timer1' is set to 10, so that when the form opens, the
'Timer1' starts to count down from 10 (in seconds).

My forms timer interval is set to 1000, and in the ontimer event, of my
form, i have:

Private Sub Form_Timer()
If Me.Timer1 = 0 Then
Else
Me.Timer1 = Me.Timer1 - 1
End If

If Me.Timer1 = 0 Then
Me.Text4 = "Check Required"
Me.Text4.BackColor = vbRed
End If

This code works well, however all i want to do is have the amount of time as
displayed in my text box (Timer1) to display in hh,mm,ss. At the moment, if
i want my 'Timer1' to allow 1.5minutes before me.textbox4 = "Check Required"
etc, then i have to set my defult value for 'Timer1' to 90 (this being
seconds), and it counts down from 90. Can i make it display as 00:01:30 ??

Any suggestions appreciated!

Thanks a lot.
 
B

Beetle

This example assumes you want the timer to start each time the
form is opened (if that's not the case it will need to modified slightly).

1) Get rid of the default value for your Timer1 text box.

2) Put the following in the declarations section of your form module
(right under Option Compare Database & Option explicit)

Dim dteStartTime as Date
Dim varCounter As Variant

3) Put the following in the Open event of your form

Private Sub Form_Open (Cancel As Integer)
dteStartTime = DateAdd("s", 90, Now())
Me.TimerInterval = 1000
Me.Text4 = ""
Me.Text4.BackColor = vbWhite
End Sub

4) Put the following in your forms Timer event

Private Sub Form_Timer()
If DateDiff("s", Now, dteStartTime) < 0 Then
Me.TimerInterval = 0
Me.Text4 = "Check Required"
Me.Text4.BackColor = vbRed
Else
varCounter = "00:0" & Int(DateDiff("s", Now, dteStartTime) / 60)
varCounter = varCounter & ":" _
& Right("0" & DateDiff("s", Now, dteStartTime) Mod 60, 2)
Me.Timer1 = varCounter
End If
End Sub

You may need to adjust for line wrap.

If you want this to happen every time you cahnge records, then use the
Current event instead of the Open event for step 3.

Good luck!

BTW - you should give Text4 a meaningful name.
 
N

Nick T

Excellent!
Thanks (again)!

Works perfectly!

Beetle said:
This example assumes you want the timer to start each time the
form is opened (if that's not the case it will need to modified slightly).

1) Get rid of the default value for your Timer1 text box.

2) Put the following in the declarations section of your form module
(right under Option Compare Database & Option explicit)

Dim dteStartTime as Date
Dim varCounter As Variant

3) Put the following in the Open event of your form

Private Sub Form_Open (Cancel As Integer)
dteStartTime = DateAdd("s", 90, Now())
Me.TimerInterval = 1000
Me.Text4 = ""
Me.Text4.BackColor = vbWhite
End Sub

4) Put the following in your forms Timer event

Private Sub Form_Timer()
If DateDiff("s", Now, dteStartTime) < 0 Then
Me.TimerInterval = 0
Me.Text4 = "Check Required"
Me.Text4.BackColor = vbRed
Else
varCounter = "00:0" & Int(DateDiff("s", Now, dteStartTime) / 60)
varCounter = varCounter & ":" _
& Right("0" & DateDiff("s", Now, dteStartTime) Mod 60, 2)
Me.Timer1 = varCounter
End If
End Sub

You may need to adjust for line wrap.

If you want this to happen every time you cahnge records, then use the
Current event instead of the Open event for step 3.

Good luck!

BTW - you should give Text4 a meaningful name.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Timer gettin complicated! 4
Timer Code 7
hh:mm:ss on a timer 2
Countdown Timer on Form 5
countdown timer 2
timer on cmd button 1
Dynamic Naming of Form Button 3
Form timer does not fire on time! 3

Top