Results 1 to 3 of 3
  1. #1
    Dougmeister is offline Novice
    Windows 10 Office 365
    Join Date
    Oct 2021
    Posts
    4

    Pause for random amount of time in loop without Access becoming unresponsive

    Looping through all records in a table, I would like to pause for a random amount of time between each record.



    Currently, I am generating a random number and calling a function that looks like this:
    Public Sub SleepVBA(Seconds)
    Dim sSeconds
    sSeconds = Seconds * 1000
    Sleep sSeconds '1000 implements a 1 second delay
    End Sub

    But the program becomes unresponsive. How can I have this time delay but still see updates that I am writing to a text box on the main form?

    Thank you.

    Edit: I found this... is this the best solution?

    https://www.fmsinc.com/microsoftacce...idDoEvents.asp

  2. #2
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,778
    I put this in a standard module so that it can be called from any procedure.
    Code:
    Function Pause(intSecs As Integer)
    Dim sngStart As Single
    
    sngStart = Timer
    Do While Timer < sngStart + intSecs
    Loop
    
    End Function
    You can change intSecs to e.g. sngSecs As Single and get pauses that are fractions of a second.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  3. #3
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,973
    There are at least three common methods for introducing a pause.
    As well as the Sleep API, you can use DoEvents or Idle DbRefreshCache.
    Each approach has advantages and diadvantages.

    i usually use DoEvents

    To help decide which is best for you, see my article Speed Comparison Tests 3 - Mendip Data Systems
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 1
    Last Post: 08-10-2016, 04:53 PM
  2. Replies: 4
    Last Post: 04-08-2015, 03:05 AM
  3. Replies: 5
    Last Post: 12-21-2013, 06:26 PM
  4. Replies: 3
    Last Post: 03-10-2013, 07:04 AM
  5. Replies: 9
    Last Post: 07-16-2010, 09:25 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums