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
![]() |
Patch reliability is unclear. Unless you have an immediate, pressing need to install a specific patch, don't do it. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
Home » Forums » AskWoody support » Productivity software by function » MS Access and database help » 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.
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
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.
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.
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
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.
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
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.
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.
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.
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.
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
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.
Notifications