Delay in VBA

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Robertf987
    New Member
    • Feb 2007
    • 23

    #1

    Delay in VBA

    Hi. I wonder if anybody can help me, I want to put a delay in my macro code. I want it to do the code, then wait say 500 miliseconds, then carry on. Can anybody help? Thanks.
  • ADezii
    Recognized Expert Expert
    • Apr 2006
    • 8834

    #2
    Originally posted by Robertf987
    Hi. I wonder if anybody can help me, I want to put a delay in my macro code. I want it to do the code, then wait say 500 miliseconds, then carry on. Can anybody help? Thanks.
    The Sleep(milliseco nds) API Routine is just what you are looking for. This Routine suspends program execution for a specified number of milliseconds. First, place this Declaration in a Standard Code Module:
    Code:
    Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    You can now call this Sub-Routine from anywhere in your application:
    Code:
    Call Sleep(5000)
    
    [B]'The Message Box will not appear for 5 seconds[/B]
    MsgBox "5 seconds have passed"

    Comment

    • Robertf987
      New Member
      • Feb 2007
      • 23

      #3
      Thanks loads for the help. I can get the delay to work for simple things like the text box, or to delay opening a form with the code you gave me. However, what I wanted it for, I can't get it to work. What I wanted was to make a progress bar. With a short delay, it would make the bar longer. I'd do it in a loop and then exit the loop once it reaches a certain length. I just want some animation to show that it is actually doing something. For some reason, I can't do this. I thought it would be simple once I know how to make a delay. I used this code:

      Code:
      Private Sub Form_Open(Cancel As Integer)
      Dim T
      T = 0
      Form.AllowDesignChanges = True
      ProgressBox.Width = T
      Do
        Call Sleep(100)
        T = T + 50
        ProgressBox.Width = T
      Loop Until T = 3500
      End Sub
      But it doesn't work. it just does nothing. Any suggestions? I can't insert an animated gif (I'm using access 2000, I can't put in an animated gif). I've searched my big Access 2000 book and there's nothing in there about progress bars.

      Comment

      Working...