Timing Delays in VBA

后端 未结 12 2238
陌清茗
陌清茗 2020-11-28 13:48

I would like a 1 second delay in my code. Below is the code I am trying to make this delay. I think it polls the date and time off the operating system and waits until the

12条回答
  •  -上瘾入骨i
    2020-11-28 14:06

    I used the answer of Steve Mallory, but I am affraid the timer never or at least sometimes does not go to 86400 nor 0 (zero) sharp (MS Access 2013). So I modified the code. I changed the midnight condition to "If Timer >= 86399 Then" and added the break of the loop "Exit Do" as follows:

    Public Function Pause(NumberOfSeconds As Variant)
        On Error GoTo Error_GoTo
    
        Dim PauseTime As Variant
        Dim Start As Variant
        Dim Elapsed As Variant
    
        PauseTime = NumberOfSeconds
        Start = Timer
        Elapsed = 0
        Do While Timer < Start + PauseTime
            Elapsed = Elapsed + 1
            If Timer >= 86399
                ' Crossing midnight
                ' PauseTime = PauseTime - Elapsed
                ' Start = 0
                ' Elapsed = 0
                Exit Do
            End If
            DoEvents
        Loop
    
    Exit_GoTo:
        On Error GoTo 0
        Exit Function
    Error_GoTo:
        Debug.Print Err.Number, Err.Description, Erl
        GoTo Exit_GoTo
    End Function
    

提交回复
热议问题