在一段时间后执行Sub?

时间:2017-06-03 09:35:14

标签: vba access-vba

我试图在UserForm打开后的一段时间后执行Sub。 Access启动时立即打开UserForm。有Application.OnTime的替代方案吗?因为它不适用于Access,似乎只能用于Excel。

这就是我正在使用的。

Private Sub UserForm_Activate()
Set objWSHShell = CreateObject("WScript.Shell")
DesktopDir = objWSHShell.SpecialFolders("Desktop") & "\"
InstallDir = Left(DesktopDir, 3) & "VICI"
VICIMxDir = InstallDir & "\VICI Mx.txt"
VICIFmDir = InstallDir & "\VICI Fm.txt"
VICIAnalyticsDir = InstallDir & "\VICI Analytics.txt"
With ListBox_Status
    If FSO.FileExists(VICIMxDir) = True Then 'Both exist
        .Column(1, 0) = "Ready"
    Else
        .Column(1, 0) = "Not Found"
    End If
    If FSO.FileExists(VICIFmDir) = True Then
        .Column(1, 1) = "Ready"
    Else
        .Column(1, 1) = "Not Found"
    End If
    If FSO.FileExists(VICIAnalyticsDir) = True Then
        .Column(1, 2) = "Ready"
    Else
        .Column(1, 2) = "Not Found"
    End If
.Column(1, 3) = Environ("UserName")
End With
Application.OnTime Now + TimeValue("00:00:10"), "DB_BackUP"
Application.OnTime Now + TimeValue("00:00:20"), "RmvDBBackup"
End Sub

1 个答案:

答案 0 :(得分:3)

您可以使用Application.Wait

Private Sub UserForm_Activate()
waitTime (3000) ' 3 seconds
MyDeleyedMethod
End Sub

Function waitTime(ByVal millsec As Double)
Application.Wait (Now() + millsec / 24 / 60 / 60 / 1000)
End Function

Private Sub MydelayedMethod()
MsgBox ("hello")
End Sub

实现这一结果的另一种方法是:

Private Sub UserForm_Activate()
Wait (3) ' 3 seconds
MyDeleyedMethod
End Sub

Sub Wait(seconds As Integer)
Dim now As Long
now = Timer()
Do
    DoEvents
Loop While (Timer < now + seconds)
End Sub

Private Sub MyDeleyedMethod()
MsgBox ("hello")
End Sub