Timer Code

N

Nick T

Hi,
With help from various.....
I have a form with various timers on it which currently only works in
minutes & seconds. I have the following code in my on timer event, however i
want my timers to be able to run into hours as well as the minutes & seconds
- any suggestions what changes i need to make to the following code in order
to 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



Thanks for any help!
 
B

Beetle

I responded to the previous thread today but I guess you didn't see it.

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
 
N

Nick T

Hi,
Think i may be biting off more than i can chew here!
Its getting a bit complicated (to me) but ill explain in the hope that you
may be able to help.

Ok, so i originally started with 2 timers - one timer1 which would count
down - on zero it would change the colour of my text box, and then timer 2
would start counting up. When a user clicks a specific cmd button it resets
timer 1 to start counting down again from a pre programmed amount ie. 2hours,
and resets the backcolour of my text box.
Now, i want to replicate this on the same form, but have 4 timers. Each one
needs to work independantly. The code you gave me worked really well,
however iv been playing to try & modify it to account for another 2 timers,
however not with much success.

Iv posted the code below which i have been playing with, in the hope that
you may be able to see where i am wanting to go with this.......(hope it
makes sence) but as i said - not having much luck. If you can help great but
let me know if i need to explain my need better:

Private Sub Form_Timer()

If DateDiff("s", Now, dteStartTime) < 0 Then
Me.Text4 = "Check Required"
Me.Text4.BackColor = vbRed
Me.Overdueby.Visible = True
Me.LastInspectionOverDueBy.Visible = False
Me.Text5 = "Check Required"
Me.Text5.BackColor = vbRed
Me.OverduebyWt.Visible = True
Me.LastInspectionOverDueByWt.Visible = False



varCounter = "00:0" & Int(DateDiff("s", dteStartTime, Now) / 60)
varCounter = varCounter & ":" _
& Right("0" & DateDiff("s", dteStartTime, Now) Mod 60, 2)
Me.Timer2 = varCounter

varCounter = "00:0" & Int(DateDiff("s", Now, dteStartTime) / 60)
varCounter = varCounter & ":" _
& Right("0" & DateDiff("s", Now, dteStartTime) Mod 60, 2)
Me.Timer4 = 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

varCounter = "00:0" & Int(DateDiff("s", dteStartTime, Now) / 60)
varCounter = varCounter & ":" _
& Right("0" & DateDiff("s", dteStartTime, Now) Mod 60, 2)
Me.Timer3 = varCounter




End If


End Sub
 
B

Beetle

That won't work because you are asking the variable varCounter to
do two different things at the same time.

Under what conditions are Timer3 and Timer4 supposed to start/stop,
and are they supposed to count up or down?
 
N

Nick T

Hi.... (thanks for reading all that) i know posts are supposed to be kept
brief!
Anyway, iv solved it - finally got there, however you may be able to tell me
if what iv done is against 'access rules' as far as good practice is
concerned:

So,
in my form code i put this in the top part:

Option Compare Database
Option Explicit
Dim dteStartTime1 As Date
Dim dteStartTime2 As Date - (this line i added)
Dim varCounter As Variant

Then in my on timer code i put this:

Private Sub Form_Timer()

If DateDiff("s", Now, dteStartTime1) < 0 Then
Me.Text4 = "Check Required"
Me.Text4.BackColor = vbRed
Me.Overdueby.Visible = True
Me.LastInspectionOverDueBy.Visible = False



varCounter = "00:0" & Int(DateDiff("s", dteStartTime1, Now) / 60)
varCounter = varCounter & ":" _
& Right("0" & DateDiff("s", dteStartTime1, Now) Mod 60, 2)
Me.Timer2 = varCounter



Else
varCounter = "00:0" & Int(DateDiff("s", Now, dteStartTime1) / 60)
varCounter = varCounter & ":" _
& Right("0" & DateDiff("s", Now, dteStartTime1) Mod 60, 2)
Me.Timer1 = varCounter

End If


If DateDiff("s", Now, dteStartTime2) < 0 Then
Me.Text5 = "Check Required"
Me.Text5.BackColor = vbRed
Me.OverduebyWt.Visible = True
Me.LastInspectionOverDueByWt.Visible = False



varCounter = "00:0" & Int(DateDiff("s", dteStartTime2, Now) / 60)
varCounter = varCounter & ":" _
& Right("0" & DateDiff("s", dteStartTime2, Now) Mod 60, 2)
Me.Timer4 = varCounter


Else
varCounter = "00:0" & Int(DateDiff("s", Now, dteStartTime2) / 60)
varCounter = varCounter & ":" _
& Right("0" & DateDiff("s", Now, dteStartTime2) Mod 60, 2)
Me.Timer3 = varCounter

End If

End Sub


Iv still got a bit of 'tidying' to do on it, but in principle i think its
working, however i still seem to have a problem with getting 'hours' on the
timers. ie if i want it to count down from 2hrs i would put in 7200 however
this comes out as 00:0119:59 and starts counting down. As i said, i have a
cmd button which when clicked, resets my timer - for info, this is my code in
that:

Private Sub FPCheck_Click()
On Error GoTo Err_FPCheck_Click

dteStartTime1 = DateAdd("s", 7200, Now())
Me.TimerInterval = 1000
Me.Text4 = ""
Me.Text4.BackColor = vbWhite
Me.Overdueby.Visible = False
Me.LastInspectionOverDueBy.Visible = True

Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "FPCheckForm"
DoCmd.OpenForm stDocName, , , stLinkCriteria

Exit_FPCheck_Click:
Exit Sub

Err_FPCheck_Click:
MsgBox Err.Description
Resume Exit_FPCheck_Click

End Sub


Hope to hear back from you - your helps been great!
Thanks
 
P

philip

Nick T said:
Hi.... (thanks for reading all that) i know posts are supposed to be kept
brief!
Anyway, iv solved it - finally got there, however you may be able to tell
me
if what iv done is against 'access rules' as far as good practice is
concerned:

So,
in my form code i put this in the top part:

Option Compare Database
Option Explicit
Dim dteStartTime1 As Date
Dim dteStartTime2 As Date - (this line i added)
Dim varCounter As Variant

Then in my on timer code i put this:

Private Sub Form_Timer()

If DateDiff("s", Now, dteStartTime1) < 0 Then
Me.Text4 = "Check Required"
Me.Text4.BackColor = vbRed
Me.Overdueby.Visible = True
Me.LastInspectionOverDueBy.Visible = False



varCounter = "00:0" & Int(DateDiff("s", dteStartTime1, Now) /
60)
varCounter = varCounter & ":" _
& Right("0" & DateDiff("s", dteStartTime1, Now) Mod 60,
2)
Me.Timer2 = varCounter



Else
varCounter = "00:0" & Int(DateDiff("s", Now, dteStartTime1) /
60)
varCounter = varCounter & ":" _
& Right("0" & DateDiff("s", Now, dteStartTime1) Mod 60,
2)
Me.Timer1 = varCounter

End If


If DateDiff("s", Now, dteStartTime2) < 0 Then
Me.Text5 = "Check Required"
Me.Text5.BackColor = vbRed
Me.OverduebyWt.Visible = True
Me.LastInspectionOverDueByWt.Visible = False



varCounter = "00:0" & Int(DateDiff("s", dteStartTime2, Now) /
60)
varCounter = varCounter & ":" _
& Right("0" & DateDiff("s", dteStartTime2, Now) Mod 60,
2)
Me.Timer4 = varCounter


Else
varCounter = "00:0" & Int(DateDiff("s", Now, dteStartTime2) /
60)
varCounter = varCounter & ":" _
& Right("0" & DateDiff("s", Now, dteStartTime2) Mod 60,
2)
Me.Timer3 = varCounter

End If

End Sub


Iv still got a bit of 'tidying' to do on it, but in principle i think its
working, however i still seem to have a problem with getting 'hours' on
the
timers. ie if i want it to count down from 2hrs i would put in 7200
however
this comes out as 00:0119:59 and starts counting down. As i said, i have
a
cmd button which when clicked, resets my timer - for info, this is my code
in
that:

Private Sub FPCheck_Click()
On Error GoTo Err_FPCheck_Click

dteStartTime1 = DateAdd("s", 7200, Now())
Me.TimerInterval = 1000
Me.Text4 = ""
Me.Text4.BackColor = vbWhite
Me.Overdueby.Visible = False
Me.LastInspectionOverDueBy.Visible = True

Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "FPCheckForm"
DoCmd.OpenForm stDocName, , , stLinkCriteria

Exit_FPCheck_Click:
Exit Sub

Err_FPCheck_Click:
MsgBox Err.Description
Resume Exit_FPCheck_Click

End Sub


Hope to hear back from you - your helps been great!
Thanks
 
B

Beetle

Try this;

Private Sub Form_Timer()

If DateDiff("s", Now, dteStartTime1) < 0 Then
Me.Text4 = "Check Required"
Me.Text4.BackColor = vbRed
Me.Overdueby.Visible = True
Me.LastInspectionOverDueBy.Visible = False

Me.Timer2 = Time - TimeValue(dteStartTime1)
Else
Me.Timer1 = Time - TimeValue(dteStartTime1)

End If


If DateDiff("s", Now, dteStartTime2) < 0 Then
Me.Text5 = "Check Required"
Me.Text5.BackColor = vbRed
Me.OverduebyWt.Visible = True
Me.LastInspectionOverDueByWt.Visible = False

Me.Timer4 = Time - TimeValue(dteStartTime2)
Else
Me.Timer3 = Time - TimeValue(dteStartTime2)

End If

End Sub


If the above works for you, then you can remove the varCounter variable
in the declarations section of your module.
 
N

Nick T

Hi Beetle,
Hope you pick this up.....

Seem to have come across somthing odd with the code you have helped me with:

If i set my timer to say 2 hrs, and there is less that 2hrs remaining of
this day, (eg the time is later than 10:00pm) then the timer reads somthing
along the lines of 23:00:00 and starts counting upwards?

It was odd, cos it worked perfectly earlier, but now it just seemed to stop
working. I turned the system clock on my pc back to allow more than 2 hrs to
be remaining of 'today' (ie before 12midnight) and it worked! Although not
too much of an issue, i thought it was odd - anything i can do to overcome
this??

Thanks
Nick
 

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

Top