+ Reply to Thread
Results 1 to 3 of 3

2 Stop Timers on User Form

Hybrid View

  1. #1
    Registered User
    Join Date
    09-22-2014
    Location
    India
    MS-Off Ver
    2003
    Posts
    7

    Unhappy 2 Stop Timers on User Form

    Hi All,

    I am trying to insert 2 stop timers on my VBA user form... One is to show login hours and other one to show break hours.
    My 1st timer is working well until I click a command button to activate 2nd timer to count break time. This command freeze 1st timer and now second timer doesn’t stop and behaves like 1st timer.
    I want to know if it’s possible to work two timers on same form. If yes how do I code that to insure one timer code does not interrupt the other one. I need timer one to be continuous and 2nd to start with last end time when started again.

    My current code goes as:

    Private Sub UserForm_Activate()
    Dim StopTimer As Boolean
    Dim Etime As Single
    Dim Etime0 As Single
    Dim LastEtime As Single
    
    StopTimer = False
    Etime0 = Timer() - LastEtime
    
    Do Until StopTimer
       Etime = Int((Timer() - Etime0) * 100) / 100
       If Etime > LastEtime Then
          LastEtime = Etime
          Label11.Caption = Format(Etime / 86400, "hh:mm:ss:") & Format(Etime * 100 Mod 100, "00")
          DoEvents
       End If
    Loop
    End Sub
    Last edited by JBeaucaire; 09-22-2014 at 01:45 PM. Reason: Added missing CODE tags. Please read and follow the Forum Rules, link above in the menu bar. Thanks.

  2. #2
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: 2 Stop Timers on User Form

    Hi VJOSHI,

    Try the following code, which is also in the attached file.

    There are global variables in the Ordinary Code module that store:
    a. If the specific timer is enabled
    b. The Start Time for that timer

    I changed the storage type to Double for the Timers, because that seems to be the data type that is usually used. My personal coding style is to put as little code in the UserForm module as possible, because it seems to be a lot easier for me to debug.

    My UserForm has the following control names that are used by the code:
    a. LabelTimer1 - Timer1 display
    b. LabelTimer2 - Timer2 display
    c. CommandButton1 - Starts Timer2
    d. CommandButton2 - Stops Timer2
    e. CommandButton3 - Resets Timer2


    UserForm1 module code:
    Option Explicit
    
    
    Private Sub UserForm_Activate()
      Call StartTimer1
      Call ProcessTimers
    End Sub
    
    Private Sub UserForm_Terminate()
      Call StopAllTimers
    End Sub
    
    Private Sub CommandButton1_Click()
      Call StartTimer2
    End Sub
    Private Sub CommandButton2_Click()
      Call StopTimer2
    End Sub
    Private Sub CommandButton3_Click()
      Call ResetTimer2
    End Sub

    Ordinary module code (such as Module1):
    Option Explicit
    
    Private bEnabledTimer1 As Boolean
    Private xStartTimeTimer1 As Double
    
    Private bEnabledTimer2 As Boolean
    Private xStartTimeTimer2 As Double
    
    Sub DisplayUserForm1()
      UserForm1.Show False
    End Sub
    
    Sub StartTimer1()
      bEnabledTimer1 = True
      xStartTimeTimer1 = Timer()
    End Sub
    
    Sub StartTimer2()
      bEnabledTimer2 = True
      xStartTimeTimer2 = Timer()
    End Sub
    
    Sub StopTimer2()
      bEnabledTimer2 = False
    End Sub
    
    Sub ResetTimer2()
      bEnabledTimer2 = False
      UserForm1.LabelTimer2.Caption = ""
    End Sub
    
    Sub StopAllTimers()
      bEnabledTimer1 = False
      Call StopTimer2
    End Sub
    
    
    Sub ProcessTimers()
    
      Dim bAtLeastOneTimerEnabled As Boolean
    
      Dim xElapsedTime As Double
      Dim LastEtime As Double
    
      bAtLeastOneTimerEnabled = True
    
      Do While bAtLeastOneTimerEnabled = True
      
        bAtLeastOneTimerEnabled = False
        
        ''''''''''''''''''''''''''''''''''''
        'Timer1 Processing
        ''''''''''''''''''''''''''''''''''''
        If bEnabledTimer1 = True Then
        
          bAtLeastOneTimerEnabled = True
      
          'Get the elapsed time
          'Allow Midnight rollover calculation
          xElapsedTime = Timer() - xStartTimeTimer1
          If xElapsedTime < 0# Then
            xElapsedTime = xElapsedTime + 86400#
          End If
      
          xElapsedTime = Int(xElapsedTime * 100) / 100
          UserForm1.LabelTimer1.Caption = Format(xElapsedTime / 86400, "hh:mm:ss:") & Format(xElapsedTime * 100 Mod 100, "00")
          DoEvents
         
        End If
        
        ''''''''''''''''''''''''''''''''''''
        'Timer2 Processing
        ''''''''''''''''''''''''''''''''''''
        If bEnabledTimer2 = True Then
        
          bAtLeastOneTimerEnabled = True
      
          'Get the elapsed time
          'Allow Midnight rollover calculation
          xElapsedTime = Timer() - xStartTimeTimer2
          If xElapsedTime < 0# Then
            xElapsedTime = xElapsedTime + 86400#
          End If
      
          xElapsedTime = Int(xElapsedTime * 100) / 100
          UserForm1.LabelTimer2.Caption = Format(xElapsedTime / 86400, "hh:mm:ss:") & Format(xElapsedTime * 100 Mod 100, "00")
          DoEvents
         
        End If
        
      Loop
      
    End Sub
    I hope this helps.

    Lewis
    Attached Files Attached Files

  3. #3
    Registered User
    Join Date
    09-22-2014
    Location
    India
    MS-Off Ver
    2003
    Posts
    7

    Re: 2 Stop Timers on User Form

    Hi Lewis,

    Thanks for this instant help. With little tweaks as per my requirement your code is working great.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] User Form to execute search and return all values to the user form for editing
    By allwrighty in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-11-2013, 10:40 PM
  2. [SOLVED] Excel vba user form- open directly to user form not worksheet
    By PANTECH in forum Excel Programming / VBA / Macros
    Replies: 26
    Last Post: 04-24-2013, 05:07 PM
  3. [SOLVED] Excel user form- If/Then statement outcome to show on user form
    By PANTECH in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-18-2013, 09:16 AM
  4. Userform - stop user saving or closing form is not complete
    By denise6372 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-18-2012, 12:24 AM
  5. calculating multiple stop/start timers within a cell
    By kmfdm515 in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 01-24-2011, 02:20 PM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1