-
Timer (loop) [RESOLVED]
Hi,
I'm trying to make a program do X every 30 seconds. My timer only fires once. Here's my code:
(in Declarations)
Code:
Option Explicit
Dim Counter As Integer
(in Form Load)
Code:
Timer1.Enabled = True
Timer1.Interval = 1000
Counter = "0"
And my sub:
Code:
Private Sub Timer1_Timer()
Counter = Counter + 1
If Counter = 30 Then
MsgBox "30 seconds has passed"
End If
End Sub
I've tried changing things around a bit, and I've tried putting Timer1.Enabled=False followed by Timer1.Enabled=True after the MsgBox.
What's wrong?
Thanks.
-
If this is all of your code, then you are missing the counter reset:
private sub timer1()
counter = counter + 1
if counter = 30 then
msgbox "30 seconds have passed, resetting counter", vbokonly
counter = 0
end if
end sub
If you don't reset the counter, your message event will never fire again.
-
Wow! Thanks for the quick reply. It worked (easy, huh). :D
-
Are you possibly declaring the variable 'counter' inside the timer event sub? If so, it gets reinitialized each iteration and will never reach 30. And if this is the case, declare the variable at form level in general declarations.
-
What's the use of the Counter variable here when you can set the Interval of the timer object to 30000.
Timer1.Interval = 30000
-
I commonly use iterative variables in timer loops for secondary functions that are not part of the timers primary function. This keeps the code footprint smaller, helps maintain memory utilization better, and helps increase speed. I was assuminmg that this is what he wanted to do. It is a common practice.