如何根据一天中的特定时间而不是每秒或每分钟触发MS Access Code [英] How to Trigger MS Access Code based on specific time of day rather than every second or minutes

查看:81
本文介绍了如何根据一天中的特定时间而不是每秒或每分钟触发MS Access Code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力做到这一点,也许我越来越近了。
我在Access中有一些代码需要在一天的特定时间运行。我不想使用Windows Task Scheduler。

I have been trying to pull this off and maybe I am getting close. I have some code in Access I need to Run at a specific time of day. I do not want to use Windows Task Scheduler.

所以我有这个功能:

So I have this function:

Public Function RunProgram()
Dim RunDateFormat As String

RunDateFormat = Format(Now(), "HH:MM:SS")
    If RunDateFormat = ("15:30:00") Then

'Call these codes and other stuff in here

End If

然后我创建了一个名为Macro1的宏来调用该函数,窗体On Timer调用了

Then I create a Macro called Macro1 to call the function, the form On Timer calls the Macro as indicated below.

问题是我是否将间隔设置为;例如:1000,我每秒收到一封电子邮件,程序保持运行状态。

The problem is if I set the interval to; example: 1000, I get an email every second and the program keeps running.

请帮助:我需要进行设置,以便代码在准确的时间运行,但只能运行一次。

Please help: I need to set this so the code runs at the exact time but only one time.

推荐答案

如何使用静态日期变量:

How about you use a static date variable:

static dateRan as date
if dateRan = Date then exit Function
If RunDateFormat = ("15:30:00") Then
dateRan = date
'Call these codes and other stuff in here
End If

这种方式可以让它保持通宵运行,并且假设没有错误发生,它将在第二天运行停止操作。

This way you could keep it running overnight and it will work the next day assuming no error occurs to halt operation.

如您所知,通过Access执行计划任务需要启动并运行表单,因此您有很大的机会无法运行您的时间敏感任务。我真的建议使用任务计划程序,如果可行的话。

As I'm sure you're aware, doing a scheduled task through Access requires the form to be up and running so you have a fairly large opportunity to fail to run the your time sensitive task. I really do recommend using the task scheduler if it is feasible.

正如HelloW所暗示的那样,除非特别重要的是,否则我不会每秒检查一次。代码在正确的时间运行。如果很关键,则可能需要进行一些检查并以编程方式修改timerinterval。

As hinted at by HelloW, I wouldn't go with every second for checking unless it is critical that the code run about the right time. If it is critical, you may want to do some checks and modify the timerinterval programmatically.

就像这样:

If Hour(Now) < 14 or Hour(Now) >= 16 then
    me.TimerInterval = 3600000
else
    me.TimerInterval = 30000
end if

您可以将其范围进一步缩小,以使检查越接近运行时间的代码就越频繁地运行

You could narrow it down more, to where you have the checks run more often the closer you get to the time you need to run your time sensitive code.

这篇关于如何根据一天中的特定时间而不是每秒或每分钟触发MS Access Code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆