Results 1 to 5 of 5

Thread: [RESOLVED] Showing timers in VBA

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2006
    Posts
    39

    Resolved [RESOLVED] Showing timers in VBA

    I have got a ten second timer working in my program using the code,

    VB Code:
    1. Application.Wait Now + TimeValue("00:00:10")

    I now want to show the seconds on screen as they are going down. I came up with the following idea to do this,

    VB Code:
    1. Application.Wait Now + TimeValue("00:00:10")
    2.        
    3.         ClockTimer = 0
    4.         Do
    5.             ClockTimer = ClockTimer + 1
    6.             TimeValue ("00:00:ClockTimer")
    7.             lblTimer.Caption = ClockTimer
    8.         Loop Until ClockTimer = 10

    i now realise how stupid this is since it waits till the ten seconds are up to run the loop, and the code doesn't work anyways, but can anyone think of a way to adapt my code slightly to make it work?

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Showing timers in VBA

    Just use a simple loop, with a one second delay for each iteration, eg:
    VB Code:
    1. Dim intCount as Integer
    2.   For intCount = 10 to 1 Step -1
    3.     lblTimer.Caption = CStr(intCount)
    4.     Application.Wait Now + TimeValue("00:00:01")
    5.   Next intCount
    6.   lblTimer.Caption = "0"

  3. #3

    Thread Starter
    Member
    Join Date
    Feb 2006
    Posts
    39

    Re: Showing timers in VBA

    Quote Originally Posted by si_the_geek
    Just use a simple loop, with a one second delay for each iteration, eg:
    VB Code:
    1. Dim intCount as Integer
    2.   For intCount = 10 to 1 Step -1
    3.     lblTimer.Caption = CStr(intCount)
    4.     Application.Wait Now + TimeValue("00:00:01")
    5.   Next intCount
    6.   lblTimer.Caption = "0"
    Thanks for the help I think that's almost it, but in my label "10" appears and then when 10 seconds have passed a "0" appears, I want it to show all the numbers.

    Any ideas what to do?

    Thanks in advance.

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Showing timers in VBA

    Throw a DoEvents in the loop to allow the CPU to update the label caption withthe changes.
    VB Code:
    1. Dim intCount as Integer
    2.   For intCount = 10 to 1 Step -1
    3.     lblTimer.Caption = CStr(intCount)
    4.     Application.Wait Now + TimeValue("00:00:01")
    5.     DoEvents
    6.   Next intCount
    7.   lblTimer.Caption = "0"
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5

    Thread Starter
    Member
    Join Date
    Feb 2006
    Posts
    39

    Re: Showing timers in VBA

    Quote Originally Posted by RobDog888
    Throw a DoEvents in the loop to allow the CPU to update the label caption withthe changes.
    VB Code:
    1. Dim intCount as Integer
    2.   For intCount = 10 to 1 Step -1
    3.     lblTimer.Caption = CStr(intCount)
    4.     Application.Wait Now + TimeValue("00:00:01")
    5.     DoEvents
    6.   Next intCount
    7.   lblTimer.Caption = "0"
    Thanks a load!

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