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

Bettany

New member
Local time
Today, 12:44
Joined
Apr 13, 2020
Messages
26
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
 

sonic8

AWF VIP
Local time
Today, 18:44
Joined
Oct 27, 2015
Messages
998
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.
 

Eugene-LS

Registered User.
Local time
Today, 19:44
Joined
Dec 7, 2018
Messages
481
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
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 00:44
Joined
May 7, 2009
Messages
19,245
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
 

Bettany

New member
Local time
Today, 12:44
Joined
Apr 13, 2020
Messages
26
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?
 

Solo712

Registered User.
Local time
Today, 12:44
Joined
Oct 19, 2012
Messages
828
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
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 12:44
Joined
Feb 19, 2002
Messages
43,296
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.

 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 11:44
Joined
Feb 28, 2001
Messages
27,191
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.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 12:44
Joined
Feb 19, 2002
Messages
43,296
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

Top Bottom