Results 1 to 11 of 11

countdown with reset

  1. #1
    slimjen is offline Expert
    Windows XP Access 2007
    Join Date
    Mar 2010
    Posts
    722

    countdown with reset


    Hi all; I have a countdown timer on the Switchboard:
    Code:
    Option Compare Database
    Option Explicit
    ‘Declare a Form Global variable to hold the
    ‘seconds expired since Form was opened.
    Dim TimeCount As Long
    Private Sub Form_Open(Cancel As Integer)
    ‘Set the Form’s Timer to 1 second intervals (1000 tics = 1 Second)
    Me.TimerInterval = 1000
    End Sub
    
    Private Sub Form_Timer()
    'Increment the TimerCount variable by 1
     TimeCount = TimeCount + 1
     'Display the current seconds remaining in the 'text box corner of form
     Me.txtCounter.Value = 60 - TimeCount
     'If the Seconds Counter (TimerCount) is now equal
     'to 40 seconds then the text color changed to Red
     If TimeCount = 40 Then
     Me.txtCounter.ForeColor = vbRed
     End If
     If TimeCount = 61 Then
     'Close the Access Program when the counter is equal to 61 seconds
     DoCmd.Quit acQuitSaveAll
    End If
    End Sub
    How can I put a Reset button to add an extra 30mins or so in case the user is working on something and just need a little more time and don't want to close and reopen. I was thinking about putting a cmdbutton on the switchboard so the user can click to add addition time but I'm not sure of the code..something like me.txtCounter.value = 60? Any suggestions?

  2. #2
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    6,354
    on click of this button you mention

    TimeCount = TimeCount + 30000 to add 30 seconds

    What is the point if user can simply override the value as many times as they want? If I click rapidly 10x, I add 10x the value you specify, such as 300 seconds.
    Last edited by Micron; 10-07-2019 at 08:41 AM. Reason: clarification and correction

  3. #3
    slimjen is offline Expert
    Windows XP Access 2007
    Join Date
    Mar 2010
    Posts
    722
    Thank you! I thought about this; that's why I disable the button after the first addition...

  4. #4
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    8,465
    What happens if I close the form with the timer event activated and open a different form?


    -----------------------------------------------------------------------------------------
    BTW, You have a statement:
    Code:
    DoCmd.Quit acQuitSaveAll
    The "SaveAll" parameter is to save CHANGES TO THE DESIGN OF THE DB", not to save the unsaved data.
    Unless you have made changes to the Db, all you need to quit Access is
    Code:
    DoCmd.Quit
    HTH
    -----
    Steve
    --------------------------------
    "Veni, Vidi, Velcro"
    (I came; I saw; I stuck around.)

  5. #5
    slimjen is offline Expert
    Windows XP Access 2007
    Join Date
    Mar 2010
    Posts
    722
    Thanks...I realized this that I copied an older code...Here's the correct code I am using now:
    Code:
    Option Compare Database
    Option Explicit
    
    Public Loops As Integer
    
    
    Private Sub Form_Load()
     Me.TimerInterval = 1000
     Form_Timer
    End Sub
    
    Private Sub Form_Timer()
    
    Static StartTime As Date
    
    Dim SecondsToCount As Integer
    Dim Min As String
    Dim Sec As String
    
    SecondsToCount = 900 'Set this variable to the total number of seconds to count down
    
    If Loops = 0 Then StartTime = Time
     
     Min = (SecondsToCount - DateDiff("s", StartTime, Time)) \ 60
     Sec = (SecondsToCount - DateDiff("s", StartTime, Time)) Mod 60
     Me.TimeLeft.Caption = "Form will close in " & Min & ":" & Format(Sec, "00")
     Loops = Loops + 1
     
     If Loops = 675 Then
     
     Me.TimeLeft.ForeColor = vbRed
    
     
    End If
    
    
     If Me.TimeLeft.Caption = "Form will close in 0:00" Then
       DoCmd.Close , ""
     End If
    
    End Sub
    I tried to use this to add time:

    Code:
    Private Sub cmdAddTime_Click()
    Loops = Loops + 30
    End Sub
    This does not work....Any suggestions?

  6. #6
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    6,354
    Time and Date are different enough to cause a problem. Date without specifying that time is part of the date results in 01/01/01 00:00:00 A.M (at least I think it is AM). Time is the clock time. Afraid I have to go back to woodworking so can't help right now, so maybe that's not all that needs to be fixed.

    Your code is difficult to read because you don't "type" your variables. Maybe you are comfortable with that from dealing with it frequently but those who have not can struggle with verifying and then trying to remember what every type is. Suggest you adopt a better naming convention to make your code easier to decipher.

    lngLong, intInteger, strString, dteDate etc (where the italicized words are data types and not part of the variable name).

    Something like one of these:

    https://access-programmers.co.uk/for...d.php?t=225837

    http://access.mvps.org/access/general/gen0012.htm

  7. #7
    slimjen is offline Expert
    Windows XP Access 2007
    Join Date
    Mar 2010
    Posts
    722
    Sorry, I adopted the code from this forum. It worked with some tweaks and I never thought to change the code for now. Thanks for pointing this out.

  8. #8
    slimjen is offline Expert
    Windows XP Access 2007
    Join Date
    Mar 2010
    Posts
    722
    Any suggestions?

  9. #9
    accesstos's Avatar
    accesstos is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Dec 2018
    Location
    Greece
    Posts
    275
    Give a try to this code:
    Code:
    Option Compare Database
    Option Explicit
    
    Private Sub Form_Load()
        Me.txtCounter = 60
        Me.txtCounter.Tag = ""
        Me.TimerInterval = 1000
    End Sub
    
    Private Sub Form_Timer()
        With Me.txtCounter
            .Value = .Value - 1
            If .Value < 1 Then Quit
        End With
    End Sub
    
    Private Sub cmdHeadstart_Click()
        With Me.txtCounter
            If .Value < 20 Then
                If .Tag = "" Then
                    'Reset remaining seconds to 30.
                    .Value = 30
                    .Tag = "Head start"
                End If
            End If
        End With
    End Sub
    Use conditional formatting for the text and background color, with an expression like [txtCounter]<20.

    Good luck with your project.

  10. #10
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    6,354
    Your prior post seemed like you were saying it was resolved so I tested your code and it appears to work. What's the issue?

  11. #11
    slimjen is offline Expert
    Windows XP Access 2007
    Join Date
    Mar 2010
    Posts
    722
    Thanks for replying..I was trying to allow for Resetting the timer but wasn't working...I now have found a work around. The code now put a countdown timer on the switchboard...changes colors(traffic light) when 30mins and 45mins elapsed. The reset button becomes enabled when the countdown label turns Red to allow resetting the timer. The reset but becomes .invisable so the reset can only happen once to avoid repeating this over n over again.
    I appreciate everyone's help...

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Countdown Timer w/ Reset on Button Click
    By ScubaSteve in forum Forms
    Replies: 1
    Last Post: 05-11-2017, 06:43 AM
  2. Countdown Date??
    By access2015 in forum Queries
    Replies: 1
    Last Post: 06-17-2015, 03:52 AM
  3. Countdown function from excel to Access
    By Andy001 in forum Access
    Replies: 1
    Last Post: 02-09-2015, 05:23 PM
  4. countdown timer and progress bar
    By yigitki in forum Programming
    Replies: 5
    Last Post: 11-16-2011, 01:20 PM
  5. countdown Timer
    By yigitki in forum Programming
    Replies: 16
    Last Post: 11-11-2011, 11:24 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Tech Forums: Microsoft Office Forums