Solved Code is executing immediately rather than in 1 second intervals.

Bettany

Member
Local time
Today, 15:17
Joined
Apr 13, 2020
Messages
44
The following code, I'd expected to take around 50 seconds, but it's executing at CPU speed and finishing instantly. What am I doing wrong? Thanks.

Code:
Sub Form_Timer()
   
    Me.TimerInterval = 1000
    Dim Y As Integer
    Dim Z As Integer
   
     
    Y = 50
    Z = 0
   
    Do While Y > Z
       
        Me.DisplayTimer = Y
        Y = Y - 1
       
    Loop
                 
    Forms!TimerLogF.TimerInterval = 0
    Call LogProcess
       
End Sub


Code:
Sub Form_Load()


    Me.TimerInterval = 0


End Sub
 
The following code, I'd expected to take around 50 seconds, but it's executing at CPU speed and finishing instantly.
Why do you expect this?
The TimerInterval affects how frequently the timer is triggered, but it has nothing to do with how the code inside the Time event procedure is executed.
 
What am I doing wrong?
Try:
Code:
Private Sub Form_Load()
    Me.TimerInterval = 1000
    Me.DisplayTimer = 50
End Sub

Private Sub Form_Timer()
    
    Me.DisplayTimer = Me.DisplayTimer - 1
    
    If Me.DisplayTimer < 1 Then
        Me.TimerInterval = 0
        Call LogProcess
    End If

End Sub
 
Code:
Dim Y As Integer

Private Sub Form_Load()
    Y = 51
    Me.TimerInterval = 1000
End Sub

Private Sub Form_Timer()
        Y = Y - 1
        Me.DisplayTimer = Y
     
    If Y <= 0 Then
                   
       Me.TimerInterval = 0
       Call LogProcess
    End If  
End Sub
 
Why do you expect this?
The TimerInterval affects how frequently the timer is triggered, but it has nothing to do with how the code inside the Time event procedure is executed.
My understanding was that once you set the timer interval to 1000ms that all code operations on the form take place at that speed. But based on what you're suggesting, the demonstrated behavior is what's expected: it's counting down from 50 to 1 in less than one second?
 
My understanding was that once you set the timer interval to 1000ms that all code operations on the form take place at that speed. But based on what you're suggesting, the demonstrated behavior is what's expected: it's counting down from 50 to 1 in less than one second.
Hi, the issue is simple. You placed the counter (Y) inside the timer loop which runs out on the first visit to the timer event and shuts down the timer. Both Eugene L-S, and arnelgp have corrected that by decrementing the counter by one on each visit. This way, the timer will be active for 50 seconds.

Best,
Jiri
 
This gives you two methods of making the code wait if that is what you are actually trying to do. It is always best if you actually tell what problem you are trying to solve.

 
My understanding was that once you set the timer interval to 1000ms that all code operations on the form take place at that speed.

Your understanding is incorrect. When you set a timer, of necessity you do it from event subroutine code (since that is the only place you can run code). That code always runs at the same speed, which is whatever speed your VBA normally runs.

Placing a value in the Me.TimerInterval slot schedules a different event, the Me.OnTimer event. If you tie a separate routine to the OnTimer event, that executes when the millisecond timer counts down to 0. If you put code in the OnTimer to reload the Me.TimerInterval slot (i.e. while still within the OnTimer event), you get a repeating form-based event that runs the OnTimer event code at regular intervals.

Timers can be a bear to work with if they are active while you are developing other code, so it is EXTREMELY important that if ever you set a timer, you must give yourself a way to shut it down, usually by loading 0 to Me.TimerInterval to prevent the next timer event.
 
Timers can be a bear to work with if they are active while you are developing other code, so it is EXTREMELY important that if ever you set a timer, you must give yourself a way to shut it down, usually by loading 0 to Me.TimerInterval to prevent the next timer event.
You only have to lose a couple hours worth of work to take this warning seriously. You, as the developer, do NOT want any timer events running unless it is the timer event that you are testing. Been there, done that, have the scars to prove it:(
 

Users who are viewing this thread

Back
Top Bottom