Access/Excel VBA - Time delay

后端 未结 2 702
终归单人心
终归单人心 2021-01-23 23:18

Note:

  1. Refresh tables in Excel that are linked to an Access database

  2. Tables in Excel need to be refreshed in order e.g Test_Sheet1, Test_Sheet2,

相关标签:
2条回答
  • 2021-01-24 00:03

    'This code pauses running code using the timer function, making special provisions for midnight (when the timer resets to 0). Implemented in MS Access

     Public Sub Pause(NumberOfSeconds As Double)
    On Error GoTo error_goto
    
    Dim PauseInterval As Variant   'Pause interval is the wait time
    Dim StartTime As Variant       'wait start time
    Dim ElapsedInterval As Variant  'time elapsed from start time to now
    Dim preMidnightInterval As Variant   'time interval from start time to midnight
    Dim endTime As Variant
    
    'initializing variables
    PauseInterval = NumberOfSeconds
    StartTime = Timer
    ElapsedInerval = 0
    preMidnightInterval = 0
    endTime = StartTime + PauseInterval
    
    Do While ElapsedInterval < PauseInterval
    ElapsedInterval = Timer - StartTime + preMidnightInterval
    'During the day premidnightInterval =0
    'shortly after midnight is passed timer is almost 0 and preMidnightInterval becomes non zero
    'detecting midnight switch
    'the instant midnight is passed ElapsedInterval = 0 - starttime + 0
        If ElapsedInterval < 0 Then
        preMidnightInterval = 86400 - StartTime 'interval segment before midnight
        StartTime = 0       'reset start time to midnight
        End If
    DoEvents
    
    Loop
    'Debug.Print "starttime " & StartTime & "elapsed interval " & ElapsedInterval & " timer:" & Timer & "endtime:" & endTime
    Exit_GoTo:
    'On Error GoTo 0
    Exit Sub
    
    error_goto:
    Debug.Print Err.Number, Err.Description, er1
    GoTo Exit_GoTo
    
    End Sub
    
    0 讨论(0)
  • 2021-01-24 00:15

    I used to use this for pausing code processing:

    Public Function Pause(intSeconds As Integer)
    
        Dim dblStart As Double
    
        If intSeconds > 0 Then
    
            dblStart = Timer()
    
            Do While Timer < dblStart + intSeconds
                ' Twiddle thumbs
            Loop
    
        End If
    End Function
    

    So you would just: Call Pause(1) wherever you need the pause at and it will wait for a second.

    Works well if you only need to delay in full second increments. I have another more robust one with more code that can be used for much smaller increments if you want it instead.

    0 讨论(0)
提交回复
热议问题