0

I wanted to run a function after 5 seconds so I tried doing:

 Application.OnTime Now + TimeValue("05:00:00"), "myFunc"

However, I get method or data member not found error. I read the documentation for this there was no reference library I needed to add.

braX
  • 11,506
  • 5
  • 20
  • 33
  • That is an Excel VBA method. Not in Access VBA. – June7 Feb 10 '20 at 02:38
  • Does this answer your question? [MS Access - Pause Program Execution](https://stackoverflow.com/questions/46105696/ms-access-pause-program-execution) – June7 Feb 10 '20 at 02:40

1 Answers1

0

Create a function for that for reuse. Use Sleep in it rather than DoEvents in cycle which eats up all the CPU.

Private Declare Sub Sleep Lib "kernel32" (ByVal lngMilliSeconds As Long)

    Public Sub WaitSeconds(intSeconds As Integer)
      Dim dTime As Date
      dTime = DateAdd("s", intSeconds, Now)
      Do
        Sleep 100
        DoEvents
      Loop Until Now >= dTime
    End Sub

Now you can call you function:

WaitSeconds 5
MyFunction
Vlado
  • 839
  • 6
  • 16
  • 1
    https://stackoverflow.com/questions/2737706/vba-sleep-doesnt-work – Andre Feb 13 '20 at 17:40
  • Sleep stops app + code execution, different to Excel, thats why you have to use a FormTimer.. – ComputerVersteher Feb 13 '20 at 17:41
  • @Andre: The link says: ".. it freezes the application.. " - this is what he wants. He wants to wait 5 seconds and then run the function - at least this is what I understood. I am using this function in several places in my application: like when an external exe does some work which takes some time (I am using pdftk.exe to merge 2 pdfs) and it works perfect. I do not think he wants to stop for 5 second, run a different code and return back. Access is not a multitasking application. – Vlado Feb 13 '20 at 18:36
  • Sorry, I should have been clearer. You need to add the API `Sleep` declaration, or your code won't work. – Andre Feb 13 '20 at 22:05