Time Interval function

Steve C

Registered User.
Local time
Today, 03:22
Joined
Jun 4, 2012
Messages
119
On a Form, how can I set a time interval between two lines of code?

To explain, here is my solution (of course, it doesn't work)

Form1 has 2 Labels (arranged to look like a traffic light) their Properties are;

LightRedLbl.Visible = False
LightGreenLbl.Visible = True

The Form Timer Interval is set to 7000

The OnTimer Event Procedure is;

LightRedLbl.Visible = True
LightGreenLbl.Visible = False

So the Traffic Light appears to turn Green to Red after seven seconds.

An AfterUpdate Event switches the Label's Visible properties back again, BUT, it rarely is seven seconds before the swap back to Red, instead, it's the remainder of seven seconds depending on the time the Update was made.

One last thing. Which is the Event Procedure which corresponds with the next record loading to the Form.

Thanks
 
The timer event will fire continuously when the form is opened. If you want it to do something specifically then you will need to leave the timer interval set to 0 and then when you want it to start, you set it in your code. So, in your After Update event you can set it like

Me.TimerInterval = 7000

and it will then fire 7 seconds from that time (you may want to have itself set its own timer interval to 0 after it does what it needs to do).
 
Thanks Boblarson.

Can I have the TimerInterval Event do one thing on one pass and another (different) thing on a second/third pass?

I tried an "If else Statement" - but the Form_Timer Event does all parts of the "If else Statement" at each Interval.

The next thing I am hoping to achieve is to get the Amber light visible, for a couple of seconds

Here's where I am at the moment.

I have a traffic light that turns from Green to Red seven seconds after a record loads to a form using this code.

Private Sub Form_Current()
Me.TimerInterval = 7000
LightRed.Visible = False
LightAmber.Visible = False
LightGreen.Visible = True

Private Sub Form_Timer()
If LightGreen.Visible = True Then
LightRed.Visible = True
LightAmber.Visible = False
LightGreen.Visible = False
End If
End Sub

P.S. The bit of code I'm most pleased with is the resetting of the TimerInterval On_Current - otherwise the Light changes at seemingly random times up to 7 seconds after the form loads
 
I'm on my phone so it is hard to type here but create a variable like this in the timer event

Static intPass As Integer

Then in the beginning of the event set it like

intPass = intPass + 1

Then use that variable in a Select Case statement so when it is a one it does one thing and a two it does something else.

And last for the third item do whatever and set the variable back to 0.
 
Thanks again Boblarson - I need to learn a whole lot about "variables" and "declaring variables" I will change my code to your suggestion - it's way cleaner than what I've come up with.

I tried an "Exit Sub" in my "If Statement" and got something which looks okay...

Private Sub Form_Current()
'reset the TimerInterval to the begining on each new record
Me.TimerInterval = 5000
'reset the Traffic light = green
LightRed.Visible = False
LightAmber.Visible = False
LightGreen.Visible = True
End Sub

Private Sub Form_Timer()
'reset the Traffic light = amber
If LightGreen.Visible = True Then
LightRed.Visible = False
LightAmber.Visible = True
LightGreen.Visible = False
'reset the TimerInterval to be shorter
Me.TimerInterval = 2000
Exit Sub
End If
'reset the Traffic light = red
If LightAmber.Visible = True Then
LightRed.Visible = True
LightAmber.Visible = False
LightGreen.Visible = False
End If
End Sub
 

Users who are viewing this thread

Back
Top Bottom