talbright
talbright

Reputation: 446

How can I slow down the execution of a macro in MS-Access

I have a macro in MS-Access that only runs correctly half the time on one computer, but when tested on another computer it runs 100% of the time.

I suspect that this is because the macro is running to fast, and not finishing the execution of one step before it moves to the next one. Is there a way I can pause between steps?

The steps for my macro are as follows:

  1. RunCode for Import-Data
  2. Open Append Query
  3. Open Append Query
  4. Open Update Query
  5. RunSQL for Delete-Data

The first step and the last step execute 100% of the time on both computers, but the middle steps sometimes fail with no error. I have tried to add this code between the macro to slow it down. I can't figure out how to apply it correctly, or if this solution is even correct.

Public Function SlowMacro()
Application.Wait (Now + TimeValue("0:00:02"))
End Function

Any help is appreciated, Thanks in advance.

Upvotes: 3

Views: 9554

Answers (1)

HansUp
HansUp

Reputation: 97131

If you want your SlowMacro() function to cause a 2 second pause, you can use code from the Access Web: API: Make code go to Sleep

Then I think this should work as your function.

Public Function SlowMacro()
    sSleep 2000
End Function

These are the key pieces from that web page I linked.

Private Declare Sub sapiSleep Lib "kernel32" _
        Alias "Sleep" _
        (ByVal dwMilliseconds As Long)

Sub sSleep(lngMilliSec As Long)
    If lngMilliSec > 0 Then
        Call sapiSleep(lngMilliSec)
    End If
End Sub

Upvotes: 3

Related Questions