Run-Time Error 1004 when Stopping Application.OnTime Procedure

JGBlue

New Member
Joined
Jun 20, 2017
Messages
4
I wrote a macro that allows a user to time the duration of their call through a UserForm, while they continue to enter data about their call on an excel sheet.
Most of the time the macro works perfectly but occasionally (randomly it seems) I get an error when attempting to pause/stop the OnTime procedure.

I receive the following error:
"Run-time error '1004':
Method 'OnTime' of object '_Application' failed"


Followed by an alert that says:
"Can't execute code in break mode"

Can someone please help me prevent this error?


The UserForm (frmTimer) contains the following code:
Code:
Private Sub cmdCall_Click()
    
[COLOR=#008000]    'START TIMER[/COLOR]
    If cmdCall.Caption = "START" Then
        cmdCall.Caption = "PAUSE"
        CallStarting = True
        CallDuration = 0
        CallStartTime = Now
        Call StartCallTimer
[COLOR=#008000]    'PAUSE TIMER[/COLOR]
    ElseIf cmdCall.Caption = "PAUSE" Then
        cmdCall.Caption = "RESUME"
        Call StopCallTimer
[COLOR=#008000]    'RESUME TIMER[/COLOR]
    ElseIf cmdCall.Caption = "RESUME" Then
        cmdCall.Caption = "PAUSE"
        CallStarting = False
        PrevCallDuration = CallDuration
        CallStartTime = Now
        Call StartCallTimer
    End If

End Sub
The following code is in a module:
Code:
Option Explicit

Public CallDuration As Date, CallStartTime As Date, PrevCallDuration As Date
Public CallStarting As Boolean


Sub StartCallTimer()
    Application.OnTime Now + TimeValue("00:00:01"), "Call_Count"
End Sub

Sub StopCallTimer()
[COLOR=#a9a9a9]     '>>>Error occasionally occurs here[/COLOR]
    Application.OnTime Now + TimeValue("00:00:01"), "Call_Count", Schedule:=False
End Sub


Sub Call_Count()

    Dim CallDisplay As String
    
    If CallStarting = True Then
        CallDuration = Now - CallStartTime
        CallDisplay = Format(CallDuration, "nn:ss")
        frmTimer.lblCallTime.Caption = CallDisplay
        Call StartCallTimer
    Else
        CallDuration = PrevCallDuration + (Now - CallStartTime)
        CallDisplay = Format(CallDuration, "nn:ss")
        frmTimer.lblCallTime.Caption = CallDisplay
        Call StartCallTimer
    End If

End Sub
 

joeu2004

Well-known Member
Joined
Mar 2, 2014
Messages
2,583
Office Version
2010
Platform
Windows
Rich (BB code):
Sub StartCallTimer()
    Application.OnTime Now + TimeValue("00:00:01"), "Call_Count"
End Sub

Sub StopCallTimer()
     '>>>Error occasionally occurs here
    Application.OnTime Now + TimeValue("00:00:01"), "Call_Count", Schedule:=False
End Sub
When you call OnTime...Schedule:=False, the first parameter (time) must be the same as the first parameter when you started the timer.

It is unlikely that Now+TimeValue("00:00:01") is the same in both calls.

Change the code as follows:
Rich (BB code):
Public StopTime As Date

Sub StartCallTimer()
     StopTime = Now + TimeValue("00:00:01")
     Application.OnTime StopTime, "Call_Count"
 End Sub

 Sub StopCallTimer()
     On Error Resume Next
     Application.OnTime StopTime, "Call_Count", Schedule:=False
 End Sub
The On Error statement is always prudent because there is a "race condition": the OnTime event might "pop" before StopCallTimer is physically running.
 
Last edited:

JGBlue

New Member
Joined
Jun 20, 2017
Messages
4
Thank you joeu2004!
I've tested it out several times and it's working flawlessly!
Greatly appreciate your help. Thank you for your time!
 

Forum statistics

Threads
1,082,343
Messages
5,364,794
Members
400,815
Latest member
gangstar67

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top