Timer gettin complicated!

N

Nick T

Hi,
With help from various, i have created a timer on my form which counts down
when the form is opened. As my db is developing, i am wanting to make an
adjustment/addition:

'Timer1' for this example is set to 10 seconds.

Basically, when 'timer 1' = 0, i want 'Timer2' to start at 0 and count up in
hh,mm,ss.

I have the following code in my forms open event:

Private Sub Form_Open(Cancel As Integer)
On Error GoTo Err_Open_Click

DoCmd.GoToRecord , , acNewRec

dteStartTime = DateAdd("s", 10, Now())
Me.TimerInterval = 1000
Me.Text4 = ""
Me.Text4.BackColor = vbWhite

Exit_Open_Click:
Exit Sub

Err_Open_Click:
MsgBox Err.Description

End Sub


and this code in my ontimer event for the form:

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


I have played around with some sample code for timers (as on the net) etc,
but cant seem to figure it out!

Any suggestions?
Thanks
 
B

Beetle

Do this;

Private Sub Form_Timer()

If DateDiff("s", Now, dteStartTime) < 0 Then
Me.Text4 = "Check Required"
Me.Text4.BackColor = vbRed
varCounter = "00:0" & Int(DateDiff("s", dteStartTime, Now) / 60)
varCounter = varCounter & ":" _
& Right("0" & DateDiff("s", dteStartTime, Now) Mod 60, 2)
Me.Timer2 = varCounter
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


Notes:

1) The line Me.TimerInterval = 0 has been removed.

2) The order of dteStartTime an Now has been reversed in the DateDiff
functions in the new part of the code.

3) If you are going to allow the second timer to increment higher than
00:09:59 then you will have to make some adjustments otherwise it
will display as 00:010:03 (etc). Also, the code is not written to
display hours,
only minutes (I don't know how long you are planning on letting your
second timer run).
 
A

Arvin Meyer [MVP]

There is only 1 timer on a form, that is the form's timer. There is no timer
control as in VB. You can, however, use the form's timer to do multiple
things. Those things can be sequential if the timer's interval is changed or
an event fires only once. If the timer's interval is the same, they can be
synchronous events. In your case the Timer's interval can be the same (1000
milliseconds).

One thing wrong with your code is that you want the start time to be less
than 0, and that will never happen, at least until you invent a time
machine. So for a new record you want Text4 to be empty with a white
background. And you also want another textbox (txtTime) to record the time.
I would open a second form as hidden at the same time as your first and use
its timer event to write to txtTime on your first form.
 
N

Nick T

Hi,
Thanks for the code - working well,

However i am wanting my timers to be able to do hours - is this an easy mod
to the code??

Thanks
Greatly appreciate your help!
 
B

Beetle

Here is a simpler, more effective method that should do everything
you want;

Private Sub Form_Timer()

If DateDiff("s", Now, dteStartTime) < 0 Then
Me.Text4 = "Check Required"
Me.Text4.BackColor = vbRed
Me.Timer2 = Time - TimeValue(dteStartTime)
Else
Me.Timer1 = Time - TimeValue(dteStartTime)
End If

End Sub
 

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 Code 7
hh:mm:ss on a timer 2
Form Timer 2
Ontimer Questions 10
Run form_timer from module 4
Use Stopwatch to trigger event 2
VBA HELP PLEASE!!!! 2
Make a variable text box flash red 4

Top