Results 1 to 13 of 13

Thread: [RESOLVED] Inconsistent Timer with GUI Library

  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

  2. #2
    Addicted Member 3com's Avatar
    Join Date
    Jul 2013
    Location
    Spain
    Posts
    253

    Re: Inconsistent Timer with GUI Library


  3. #3

    Thread Starter
    Member
    Join Date
    Apr 2014
    Posts
    35

    Re: Inconsistent Timer with GUI Library

    Thank you for your input. Took a look at it.
    I was stopping the timer the same way as that page with:
    Code:
     Application.OnTime NextTrigger, "TimerModule.OnTimer", Schedule:=False
    I have no clue why this does not work 100% of the time. Also, isn't putting an
    Code:
     On Error Resume Next
    Bad practice when stopping the timer? Because if it does not work, then wouldn't the timer still be running the rest of the time Excel is open?

  4. #4

    Thread Starter
    Member
    Join Date
    Apr 2014
    Posts
    35

    Re: Inconsistent Timer with GUI Library

    Sorry, just to be concise, this is the code that handles the timer in UserForm1:
    Code:
    '''''''''''''' Timer Processes Defined for this Procedure '''''''''''''''''
    Private Sub StartTimer_State()
        Application.OnTime NextTrigger, "TimerModule.OnTimer", Schedule:=True
        iStart = Now
    End Sub
    
    Private Function NextTrigger()
        NextTrigger = Now + TimeValue("00:00:01")
    End Function
    
    Public Sub OnTimer()
        ' Schedules itself per second while updating timerLabel with elapsed time
        Application.OnTime NextTrigger, "TimerModule.OnTimer", Schedule:=True
        ProcPerSecond
    End Sub
    
    Private Sub StopTimer_State()
        Application.OnTime NextTrigger, "TimerModule.OnTimer", Schedule:=False
    End Sub
    
    Private Sub ProcPerSecond()
        ' display time elapsed on timerLabel.
        Me.timerLabel = Format(Now - iStart, "hh:mm:ss")    
    End Sub
    TimerModule.OnTimer calls OnTimer in UserForm1.

  5. #5
    Addicted Member 3com's Avatar
    Join Date
    Jul 2013
    Location
    Spain
    Posts
    253

    Re: Inconsistent Timer with GUI Library

    Should I assume "TimerModule" as standard module, containing a sub called "OnTimer" ????
    If so, what that macro does?

    Related info... http://msdn.microsoft.com/en-us/libr...ice.14%29.aspx

  6. #6

    Thread Starter
    Member
    Join Date
    Apr 2014
    Posts
    35

    Re: Inconsistent Timer with GUI Library

    Yessir! This is what TimerModule has:

    Code:
     
    Private Sub OnTimer()
        UserForm1.OnTimer
    End Sub

  7. #7
    Addicted Member 3com's Avatar
    Join Date
    Jul 2013
    Location
    Spain
    Posts
    253

    Re: Inconsistent Timer with GUI Library

    Does "TimerModule.OnTimer" launch the application?
    If so, ¿how do you load form?
    Who call to "StopTimer_State" Sub ?, a commandbutton?
    And, do you want to close form when timer being stopped?

    You has said above that you try sub several times (60/40%) getting error, but what system do you use?
    In another words:
    a-You launch application
    b-You stop the chrono.
    c-you close form

    After this, you reopen the application and restart the process as above?

  8. #8

    Thread Starter
    Member
    Join Date
    Apr 2014
    Posts
    35

    Re: Inconsistent Timer with GUI Library

    UserForm1 is part of a family of UserForms. Another UserForm launches it. TimerModule.OnTimer calls OnTime in the UserForm as a workaround for the Application.OnTime restriction that the Procedure called in:
    Code:
     Application.OnTime(EarliestTime, Procedure, LatestTime, Schedule)
    must be in a Module for OnTime to work.

    The actual code I have for this Userform has 8 "states" that rotate between different GUI formats. The "states" hide and show different Labels, TextBoxes, and ComboBoxes. To put more succintly, I am using a pseudo state-machine. One particular state, the Timer State, is being shown twice in the Userform.

    StartTimer_State and StopTimer_State are called using one Button: NextButton, and the Timer States are enabled when the variable enable_sm is set to False.

    I have tested the timer by cycling through the several "states". This is when I get the 60/40% error.
    So I test it like this:
    a - cycle through other states til I reach Timer state
    b - start timer
    c - stop timer
    d - repeat a-c

    I very much appreciate your patience in understanding my problem, 3com!

  9. #9
    Addicted Member 3com's Avatar
    Join Date
    Jul 2013
    Location
    Spain
    Posts
    253

    Re: Inconsistent Timer with GUI Library

    Well here 23:18 pm, tomorrow has to work, well tomorrow I or any other user, take at look...

  10. #10

    Thread Starter
    Member
    Join Date
    Apr 2014
    Posts
    35

    Re: Inconsistent Timer with GUI Library

    Thank you sir. I appreciate the help!

  11. #11
    Addicted Member 3com's Avatar
    Join Date
    Jul 2013
    Location
    Spain
    Posts
    253

    Re: Inconsistent Timer with GUI Library

    I have a form and a standard module.
    A textbox and commandbutton.
    NextButton changes their caption between "start" and "stop" depending on the state of the timer.
    When I press the NextButton the timer is reset. If I press the button again, the timer stops.
    I have tried many times without any failure.

    Userform1 code...

    Code:
    Dim iCount As Integer
    Dim timerStart As Boolean
    
    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
    
    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
                NextButton.Caption = "Stop"
                Me.timerLabel = "00:00:00"
                StartTimer_State
            Else
                NextButton.Caption = "Start"
                StopTimer_State
            End If
        End If
    End Sub
    
    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
    HTH

  12. #12

    Thread Starter
    Member
    Join Date
    Apr 2014
    Posts
    35

    Re: Inconsistent Timer with GUI Library

    WOW! Thank you very much! So it was the variable declaration causing me the trouble... haha but that works perfectly now. Again, thank you!

  13. #13
    Addicted Member 3com's Avatar
    Join Date
    Jul 2013
    Location
    Spain
    Posts
    253

    Re: Inconsistent Timer with GUI Library

    Glad you get solved.
    Please come back and mark thread as resolved.

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