Results 1 to 3 of 3

Thread: Countdown Timer

  1. #1
    2 Star Lounger
    Join Date
    Jun 2002
    Location
    Barnsley, Yorkshire, England
    Posts
    108
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Countdown Timer

    Trying to make a countdown timer and have written the following code:

    Dim intCounter As Integer
    intCounter = 60
    Do While intCounter > 0
    Me!lblCountdown.Caption = intCounter
    intCounter = intCounter - 1
    Loop

    Timer Interval for the form is set to 1000

    There will also be code to display "Time Up" or similar.

    My problem is that it immediately displays 1, suggesting that the code is not looping once per second, however the looping is working if i step through the code (intCounter reduces by one each loop)

    Where am i going wrong???

    Thanks

  2. #2
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: Countdown Timer

    Hi Peter,
    What you need to do is something like the following (you've probably already done some of this):
    1. Add a textbox (e.g. txtTimer) to hold the timer value
    2. Add code to the form Load event to set the inital value:
    - e.g. Me!txtTimer = 60
    3. Set TimerInterval to required value (in your case 1000)
    4. Add code to the OnTimer event of the form to decrease the textbox value by 1 and check if it has reached zero - e.g.
    Private Sub Form_Timer()
    With Me
    !txtTimer = !txtTimer - 1
    If !txtTimer = 0 Then
    MsgBox "Time up!"
    .TimerInterval = 0
    End If
    End With
    End Sub
    Hope that helps.
    Regards,
    Rory

    Microsoft MVP - Excel

  3. #3
    2 Star Lounger
    Join Date
    Jun 2002
    Location
    Barnsley, Yorkshire, England
    Posts
    108
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Countdown Timer

    Rory. That's great thank you. I can see now that my code was running in it's entirety on the timer event, and not one loop at a time. Great!

Posting Permissions

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