Access VBA: 表单计时器未在预期时间显示 MsgBox
Access VBA: Form timer not showing MsgBox at the intended time
我试图让数据库在 2 分钟后关闭(出于测试目的,我只是在 2 分钟后显示 MsgBox
)。为此,我有一个名为 DTForm
的主窗体和一个名为 Timer
的隐藏窗体。两种形式都在打开数据库时打开,但 Timer
以隐藏模式打开。
AutoExec 宏:
1. Open DTForm (the main form)
2. Open Timer (the hidden form)
模块 1:
Option Compare Database
Option Explicit
Public timer_start As Date
Public timer_end As Date
Public timer_diff As Integer
DTForm(用户只会看到这个表格)
Option Compare Database
Option Explicit
Public Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
timer_start = Time
timer_end = DateAdd("n", 2, timer_start)
End Sub
计时器(隐藏形式):
Option Compare Database
Option Explicit
Public Sub Form_Open(Cancel As Integer)
timer_start = Time
timer_end = DateAdd("n", 2, Time)
End Sub
Public Sub Form_Load()
timer_start = Time
timer_end = DateAdd("n", 2, Time)
End Sub
Public Sub Form_Timer()
timer_diff = DateDiff("n", timer_end, Time)
If timer_diff >= 0 Then
'Application.Quit
MsgBox "timer reached 0"
timer_start = Time
timer_end = DateAdd("n", 2, Time)
End If
End Sub
更新 - 问题: 所以现在的问题是鼠标。看起来只是在窗体上移动鼠标没有任何作用。 但是,将鼠标从导航窗格移动到窗体并返回(进出) 会触发鼠标移动。这似乎真的违反直觉——为什么不只考虑所有的鼠标移动呢?
注意事项: 在两种形式的顶部添加了 Option Explicit
并修复了一些缺失的变量声明。
这对我来说似乎是一个可变范围问题。默认情况下,您的变量是本地的。
除非 timer_end 被声明为全局变量或 public 变量,否则一旦您离开子定义它的位置,它就会超出范围。因此 Form_timer 中的 timer_end 是一个与 MouseMove 事件中的变量完全不同的变量(即使它们具有相同的名称)。
这就是许多人将 "option explicit" 放在代码开头的原因之一,因为它会强制您声明变量。
您还可以采用一种设计策略,将变量作为参数传递,而不是将其设为全局变量或 public
DateDiff("s", timer_end, Time)
会return一个负值,直到10秒不活动,这时条件必须改为>=
,时间间隔改为1000(1秒)1毫秒太快了。
还要确保变量正常添加 option explicit
子句
Option Compare Database
Option Explicit
Dim timer_start as Variant
Dim timer_end as Variant
Public Sub Form_Open(Cancel As Integer)
timer_start = Time
End Sub
Public Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
timer_start = Time
timer_end = DateAdd("s", 10, timer_start)
Me.Label6.Caption = timer_start
Me.Label8.Caption = timer_end
End Sub
Public Sub Form_Timer()
If DateDiff("s", timer_end, Time) >= 0 Then
MsgBox "timer reached 0"
End If
End Sub
如果你想完成它,Form_MouseMove
事件必须在每个窗体中,因为只有活动窗体接收 MouseMove 事件。
注意:实际上您根本不需要 timer_start
变量。您可以将其删除并仅保留:
Public Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
timer_end = DateAdd("n", 2, Time)
End Sub
或更清洁一些:
Public Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Call UpdateTimer()
End Sub
在您的模块 1 中:
Public Sub UpdateTimer()
timer_end = DateAdd("n", 2, Time)
End Sub
如果您决定更改时间间隔...
注:
timer_diff
应该是 Timer.Form_Timer()
中的局部变量,因为它只在那里使用。
我试图让数据库在 2 分钟后关闭(出于测试目的,我只是在 2 分钟后显示 MsgBox
)。为此,我有一个名为 DTForm
的主窗体和一个名为 Timer
的隐藏窗体。两种形式都在打开数据库时打开,但 Timer
以隐藏模式打开。
AutoExec 宏:
1. Open DTForm (the main form)
2. Open Timer (the hidden form)
模块 1:
Option Compare Database
Option Explicit
Public timer_start As Date
Public timer_end As Date
Public timer_diff As Integer
DTForm(用户只会看到这个表格)
Option Compare Database
Option Explicit
Public Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
timer_start = Time
timer_end = DateAdd("n", 2, timer_start)
End Sub
计时器(隐藏形式):
Option Compare Database
Option Explicit
Public Sub Form_Open(Cancel As Integer)
timer_start = Time
timer_end = DateAdd("n", 2, Time)
End Sub
Public Sub Form_Load()
timer_start = Time
timer_end = DateAdd("n", 2, Time)
End Sub
Public Sub Form_Timer()
timer_diff = DateDiff("n", timer_end, Time)
If timer_diff >= 0 Then
'Application.Quit
MsgBox "timer reached 0"
timer_start = Time
timer_end = DateAdd("n", 2, Time)
End If
End Sub
更新 - 问题: 所以现在的问题是鼠标。看起来只是在窗体上移动鼠标没有任何作用。 但是,将鼠标从导航窗格移动到窗体并返回(进出) 会触发鼠标移动。这似乎真的违反直觉——为什么不只考虑所有的鼠标移动呢?
注意事项: 在两种形式的顶部添加了 Option Explicit
并修复了一些缺失的变量声明。
这对我来说似乎是一个可变范围问题。默认情况下,您的变量是本地的。
除非 timer_end 被声明为全局变量或 public 变量,否则一旦您离开子定义它的位置,它就会超出范围。因此 Form_timer 中的 timer_end 是一个与 MouseMove 事件中的变量完全不同的变量(即使它们具有相同的名称)。
这就是许多人将 "option explicit" 放在代码开头的原因之一,因为它会强制您声明变量。
您还可以采用一种设计策略,将变量作为参数传递,而不是将其设为全局变量或 public
DateDiff("s", timer_end, Time)
会return一个负值,直到10秒不活动,这时条件必须改为>=
,时间间隔改为1000(1秒)1毫秒太快了。
还要确保变量正常添加 option explicit
子句
Option Compare Database
Option Explicit
Dim timer_start as Variant
Dim timer_end as Variant
Public Sub Form_Open(Cancel As Integer)
timer_start = Time
End Sub
Public Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
timer_start = Time
timer_end = DateAdd("s", 10, timer_start)
Me.Label6.Caption = timer_start
Me.Label8.Caption = timer_end
End Sub
Public Sub Form_Timer()
If DateDiff("s", timer_end, Time) >= 0 Then
MsgBox "timer reached 0"
End If
End Sub
如果你想完成它,Form_MouseMove
事件必须在每个窗体中,因为只有活动窗体接收 MouseMove 事件。
注意:实际上您根本不需要 timer_start
变量。您可以将其删除并仅保留:
Public Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
timer_end = DateAdd("n", 2, Time)
End Sub
或更清洁一些:
Public Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Call UpdateTimer()
End Sub
在您的模块 1 中:
Public Sub UpdateTimer()
timer_end = DateAdd("n", 2, Time)
End Sub
如果您决定更改时间间隔...
注:
timer_diff
应该是 Timer.Form_Timer()
中的局部变量,因为它只在那里使用。