Timing Delays in VBA

后端 未结 12 2214
陌清茗
陌清茗 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条回答
  • 2020-11-28 13:58

    I use this little function for VBA.

    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 = 0 Then
                ' Crossing midnight
                PauseTime = PauseTime - Elapsed
                Start = 0
                Elapsed = 0
            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
    
    0 讨论(0)
  • 2020-11-28 14:02

    Have you tried to use Sleep?

    There's an example HERE (copied below):

    Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    
    Private Sub Form_Activate()    
    
    frmSplash.Show
    DoEvents
    Sleep 1000
    Unload Me
    frmProfiles.Show
    
    End Sub
    

    Notice it might freeze the application for the chosen amount of time.

    0 讨论(0)
  • 2020-11-28 14:02

    Access can always use the Excel procedure as long as the project has the Microsoft Excel XX.X object reference included:

    Call Excel.Application.Wait(DateAdd("s",10,Now()))
    
    0 讨论(0)
  • 2020-11-28 14:04

    The Timer function also applies to Access 2007, Access 2010, Access 2013, Access 2016, Access 2007 Developer, Access 2010 Developer, Access 2013 Developer. Insert this code to to pause time for certain amount of seconds

    T0 = Timer
    Do
        Delay = Timer - T0
    Loop Until Delay = 1 'Change this value to pause time in second
    
    0 讨论(0)
  • 2020-11-28 14:05

    On Windows timer returns hundredths of a second... Most people just use seconds because on the Macintosh platform timer returns whole numbers.

    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题