Free Lessons
Courses
Seminars
TechHelp
Fast Tips
Templates
Topic Index
Forum
ABCD
 
Home   Courses   Index   Templates   Seminars   TechHelp   Forums   Help   Contact   Join   Order   Logon  
 
Access Tip Sleep Timer
By Richard Rost   Richard Rost on LinkedIn Email Richard Rost   4 years ago

Want to add a delay in your Access VBA code? This is sometimes handy if you want to give the user time to react, or slow down processing so you can see what's going on, or even wait for a background process to finish. 

First, declare the following at the top of your module:

#If VBA7 Then
    Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)
#Else
    Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
#End If

This is a Windows API function that you can use to pause a particular number of milliseconds. So:

Sleep 1000

would have processing sit there for a full second not doing anything. This function by itself locks up the whole computer and doesn't allow any input for that second, which is fine if it's only a second, but if you want, say, a 15 second pause, you're locking up the computer for a whole 15 seconds. So I created this function to go with it:

Public Sub mySleep(Optional Seconds As Double = 1, Optional AllowDoEvents As Boolean = True)
    ' this function will break up seconds into quarters of a second
    Dim SecFrac As Double
    SecFrac = 0
    While SecFrac < Seconds
        Sleep 250
        If AllowDoEvents Then DoEvents
        SecFrac = SecFrac + 0.25
    Wend
End Sub

What this does is break up the timer into quarter second chunks (250 milliseconds) and the DoEvents code allows processing to continue if something else is happening in the background. It also allows you to put an ABORT button or checkbox on your form to give the user a chance to cancel it.

Now you can say:

While Not Abort
    ' Do some stuff here
    mySleep 5 ' sleep for 5 seconds
Wend

If anyone would like to see me make a video lesson about this, let me know.

 

 

Start a NEW Conversation
 
Only students may post on this page. Click here for more information on how you can set up an account. If you are a student, please Log On first. Non-students may only post in the Visitor Forum.
 
Subscribe
Subscribe to Access Tip Sleep Timer
Get notifications when this page is updated
 
 
 
 

The following is a paid advertisement
Computer Learning Zone is not responsible for any content shown or offers made by these ads.
 

Learn
 
Access - index
Excel - index
Word - index
Windows - index
PowerPoint - index
Photoshop - index
Visual Basic - index
ASP - index
Seminars
More...
Customers
 
Login
My Account
My Courses
Lost Password
Memberships
Student Databases
Change Email
Info
 
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Search The Site
Code Vault
Collapse Menus
Help
 
Customer Support
Web Site Tour
FAQs
TechHelp
Consulting Services
About
 
Background
Testimonials
Jobs
Affiliate Program
Richard Rost
Free Lessons
Mailing List
PCResale.NET
Order
 
Video Tutorials
Handbooks
Memberships
Learning Connection
Idiot's Guide to Excel
Volume Discounts
Payment Info
Shipping
Terms of Sale
Contact
 
Contact Info
Support Policy
Mailing Address
Phone Number
Fax Number
Course Survey
Email Richard
[email protected]
Blog RSS Feed    YouTube Channel

LinkedIn
Copyright 2024 by Computer Learning Zone, Amicron, and Richard Rost. All Rights Reserved. Current Time: 3/28/2024 6:18:44 AM. PLT: 1s
Keywords: access tips sleepsec sleep timer delay  PermaLink  Access Tip Sleep Timer