• Delays in VBA (2003)

    Author
    Topic
    #409165

    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

    Viewing 3 reply threads
    Author
    Replies
    • #869961

      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.

      • #870111

        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

        • #870129

          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.

          • #870163

            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

          • #870164

            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

        • #870130

          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.

      • #870112

        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

    • #869962

      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.

    • #869991

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

      Option Explicit

      Public Declare Function Sleep Lib "kernel32" _
      (ByVal dwmilliseconds As Long) As Long

      Sub TestSleep()

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

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

      End Sub

      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

      • #870003

        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. shrug

        • #870151

          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.

          • #870155

            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.

            • #870189

              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.

            • #870190

              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.

          • #870156

            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.

        • #870152

          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.

      • #870004

        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. shrug

    • #869992

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

      Option Explicit

      Public Declare Function Sleep Lib "kernel32" _
      (ByVal dwmilliseconds As Long) As Long

      Sub TestSleep()

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

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

      End Sub

      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

    Viewing 3 reply threads
    Reply To: Delays in VBA (2003)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: