Results 1 to 10 of 10
  1. #1
    Star Lounger
    Join Date
    Jan 2003
    Location
    Manchester, Lancashire, England
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Delays in VBA (2003)

    Is there a generally applicable way of coding a delay in VBA? I want to open a form, leave it open for say 2 secs & then close it. I can do it all but code the delay.
    I suppose that I could use a for loop but thats not elegant!

    TIA

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Delays in VBA (2003)

    You can use the On Timer event of the form, together with the Timer Interval property.
    Set the Timer Interval property to the number of milliseconds you want to wait, so for example 2000 for 2 seconds.
    Put code in the On Timer event to close the form:

    Private Sub Form_Timer()
    DoCmd.Close acForm, Me.Name
    End Sub

    Note: if you don't close the form, the On Timer event will recur every Timer Interval milliseconds, until you either close the form or set Me.TimerInterval = 0 in code; this disables the On Timer event.

  3. #3
    Bronze Lounger
    Join Date
    Nov 2001
    Location
    Arlington, Virginia, USA
    Posts
    1,394
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Re: Delays in VBA (2003)

    Another option (useful if not using form, and therefore Timer event not available) is to use Windows Sleep API function. Example:

    <code>Option Explicit</code>

    <code>Public Declare Function Sleep Lib "kernel32" _</code>
    <code> (ByVal dwmilliseconds As Long) As Long</code>

    <code>Sub TestSleep()</code>

    <code> ' Pause 2 seconds:</code>
    <code> Sleep 2000</code>
    <code> MsgBox "Wake up!", vbExclamation, "SLEEP TEST"</code>

    <code> ' Pause another 2 seconds:</code>
    <code> Sleep 2000</code>
    <code> MsgBox "Go back to sleep!", vbExclamation, "SLEEP TEST"</code>

    <code>End Sub</code>

    Copy this code into standard module and run the test sub. A noticeable 2-second delay will occur between the msgboxes. I'd recommend only use Sleep for short durations; user a Timer event for longer durations.

    HTH

  4. #4
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Delays in VBA (2003)

    That's the code we prefer in our apps because form timers in Access tend to interfere with other operations and they don't appreciate your cancelling something. <img src=/S/shrug.gif border=0 alt=shrug width=39 height=15>
    Charlotte

  5. #5
    Star Lounger
    Join Date
    Jan 2003
    Location
    Manchester, Lancashire, England
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Delays in VBA (2003)

    Brent in the Utter Access forum suggested the following code as a general solution to my problem - it seems to be perfect!

    Public Sub Pause(intWaitTime as Integer)
    Dim dtTimerExpired as Date
    dtTimerExpired = DateAdd("s",intWaitTime,Now())
    Do Until Now() >= dtTimerExpired
    DoEvents
    Loop
    End Sub

  6. #6
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Delays in VBA (2003)

    There is nothing wrong with that code and it's typically what you use in a form_timer event. The Sleep API call doesn't require a form_timer. For instance, we use it when we are monitoring a registry key to see whether a value has been added to it when signalling between applications. If we don't find that value, we wait for the specified sleep interval and then try again.
    Charlotte

  7. #7
    Bronze Lounger
    Join Date
    Nov 2001
    Location
    Arlington, Virginia, USA
    Posts
    1,394
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Re: Delays in VBA (2003)

    According to the documentation, the Sleep function "suspends operation of a thread for the specified time," meaning the current thread of execution in the current process (which in Access, would be the current running instance of Access). So in theory, calling Sleep function should not interfere with other processes that are running. I've always found Form Timer events somewhat flakey so tend to avoid them.

  8. #8
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Delays in VBA (2003)

    I never use the Timer event for forms in which the user can enter data, but it seems to work fine for a splash screen, or a form that contains only a command button.

  9. #9
    Star Lounger
    Join Date
    Jan 2003
    Location
    Manchester, Lancashire, England
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Delays in VBA (2003)

    Charlotte
    Thanks for your comment but you are so far beyond my level that you are not only out of sight you are beyond my event horizon! I cannot even imagine why I would want to monitor a registry key let alone how I would do it!!

    Ian

  10. #10
    Bronze Lounger
    Join Date
    Nov 2001
    Location
    Arlington, Virginia, USA
    Posts
    1,394
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Re: Delays in VBA (2003)

    In fact the only time I've used Timer event in actual applications is for a splash screen that is displayed for a few seconds when application opens, then goes away (using a bitmap for this purpose used to work till machines got too fast). When first using Access & VBA experimented with Timer events for such essential design elements as flashing text, animated images, etc., but quickly abandoned these efforts when the extremely annoying nature of these dubious user interface "enhancements" became evident.

Posting Permissions

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