Results 1 to 13 of 13

Thread: [RESOLVED] Inconsistent Timer with GUI Library

Threaded View

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2014
    Posts
    35

    Resolved [RESOLVED] Inconsistent Timer with GUI Library

    Code:
    Option Explicit
    ''''''''''''''''''''''''''''''' In Userform ''''''''''''''''''''''''''''''''''''''''''''''''''''
    Private iCount As Integer
    
    Private Sub NextButton_Click()
        If enable_SM Then
            ' If State Transitions is True, proceed
            iCount = iCount + 1
            Call ProcStep(iCount)
        Else
            ' If State Transitions is False, go to Timer State. 
            ' Clicking NextButton will start and stop the timer.
            If timerStart = False Then
                Me.timerLabel = "00:00:00"
                StartTimer_State
            Else
                StopTimer_State
            End If
        End If
    End Sub
    
    '''''''''''''' Timer Processes Defined for this Procedure '''''''''''''''''
    Private Sub StartTimer_State()
        Application.OnTime NextTrigger, "TimerModule.OnTimer", Schedule:=True
        timerStart = True                  ' timerStart is a Boolean that indicates if timer has started
        iStart = Now
    End Sub
    
    Private Function NextTrigger()
        NextTrigger = Now + TimeValue("00:00:01")
    End Function
    
    Public Sub OnTimer()
        Application.OnTime NextTrigger, "TimerModule.OnTimer", Schedule:=True
        ProcPerSecond
    End Sub
    
    Private Sub StopTimer_State()
        Application.OnTime NextTrigger, "TimerModule.OnTimer", Schedule:=False
        timerStart = False
        enable_SM = True
    End Sub
    
    Private Sub ProcPerSecond()
        Me.timerLabel = Format(Now - iStart, "hh:mm:ss")
    End Sub
    
    ''''''''''''''''''''''''''' State Library ''''''''''''''''''''''''''''''
    Private Sub ProcStep(iProcedure As Integer)
    ' GUI Library
        Select Case iProcedure
        Case 0
            Call State0
        Case 1
            enable_sm = False         ' Begin Timer State
        Case Else
            Unload Me
        End Select
    End Sub
    
    Private Sub State0()
        ' arbitrary code here
    End Sub
    
    '''''''''''''''' In TimerModule: ''''''''''''''''''''
    Private Sub OnTimer()
        UserForm1.OnTimer
    End Sub
    Hi guys,
    I'm fairly new to Visual Basic (and to the VB Forums - even posted this in the wrong section!), but I'd really appreciate it if anyone can help me and/or point me in the right direction. This is run in MS Excel 2007.
    I've omitted irrelevant parts of the code, but I basically have a GUI Library that rotates through 5 "states". One of the states will have a timer that counts up in the "hh:mm:ss" format, updated each second.

    The problem is, stopping the timer have been inconsistent. It works about 60% of the time when I'm testing it, but the other 40% I get this error:

    Run-time Error '1004':
    Method 'OnTime' of object '_Application' failed

    And I have no idea why. Shouldn't:
    Code:
    Application.OnTime NextTrigger, "TimerModule.OnTimer", Schedule:=False
    Adequately stop any timer procedures being executed? Why is it a hit-or-miss?
    Last edited by ThreadHelp; Apr 8th, 2014 at 01:51 PM. Reason: Indicated I am using MS Excel

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width