vba 每 30 分钟运行一次代码的计时器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23757801/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Timer to run code every 30 minutes
提问by Challenger
Since I`m in a develop mode and my front ends requires a lot of updates. This is my function to check the version when the user try to open on of the front end:
由于我处于开发模式并且我的前端需要大量更新。这是我在用户尝试打开前端时检查版本的功能:
Public Function FEPath(DBName As String)
Dim Conn As New ADODB.Connection
Dim DataConn As New ADODB.Recordset
Dim Comm As String
Dim strPath As String
Dim strDB As String
Dim strVer As String
Dim strExt As String
Comm = " SELECT tblFE.Database, tblFE.Version, tblFE.Extension " & _
" FROM tblFE WHERE tblFE.[Database] = '" & DBName & "';"
Set Conn = CurrentProject.Connection
DataConn.Open Comm, Conn, adOpenKeyset, adLockOptimistic
With DataConn
strDB = ![Database]
strVer = ![Version]
strExt = ![Extension]
strPath = "C:\Databases\" & strDB & " " & strVer & strExt
.Close
End With
Set Conn = Nothing
I need to somehow run a piece of code every 30 minutes, while the front end is open, to check to see if there is a new front end version available for upload. I would then notify the user, close the database, and update to the new version. Right now, version updating is always happening when the database opens. How can this be done?
我需要以某种方式每 30 分钟运行一段代码,同时前端打开,以检查是否有新的前端版本可供上传。然后我会通知用户,关闭数据库,并更新到新版本。现在,版本更新总是在数据库打开时发生。如何才能做到这一点?
Thank you.
谢谢你。
回答by RubberDuck
There's an old trick MS Access programmers use. You use an autoexec macro to open a hidden form when your database launches. Then you can use the Form_Timer eventto trigger any code that you want to run every so often.
MS Access 程序员使用了一个古老的技巧。当您的数据库启动时,您可以使用 autoexec 宏打开一个隐藏的表单。然后您可以使用Form_Timer 事件触发您想要经常运行的任何代码。
The TimerInterval form propertyuses milliseconds, so you could either set it manually to 1800000
(1000 * 60 * 30) or use your hidden form's on_load event to set it. I usually opt for option 2 so it's spelled out in the code somewhere.
该TimerInterval窗体属性用途毫秒,所以你可以手动将其设置为1800000
(1000 * 60 * 30),或使用你的隐藏表单的ON_LOAD事件来设置它。我通常选择选项 2,所以它在代码中的某处有详细说明。
Sub Form_Load()
Me.TimerInterval = 1000 * 60 * 30 ' 30 minutes
End Sub
Sub Form_Timer()
' check the front end version here
End Sub
*Note: Setting the TimerInterval
in the load event also allows you to set it to 0 (effectively turn it off) when you're developing. I don't expect you'll need to with a 30 minute timer, but when you're running something every 1/4 second, form timers can be a pain to work with while developing other places in your database application.
*注意:TimerInterval
在加载事件中设置 还允许您在开发时将其设置为 0(有效地将其关闭)。我不希望你需要一个 30 分钟的计时器,但是当你每 1/4 秒运行一次时,在你的数据库应用程序中开发其他地方时,使用表单计时器可能会很痛苦。