我有MS Access 2003 DB。
表单上的按钮的事件处理程序是否有可能触发两次?
[我似乎有薪资过程的证据,记录整个过程的过程,并且过程在日志中重复。
我认为这在VBA中是不可能的?
编辑:我发现确实触发了两次,因为用户单击两次并两次将事件排队。这是我对代码所做的修复,该代码使用标志m_locked作为示例进行测试:
[代码]
Private m_locked As Boolean
Private m_count As Integer
Private Sub Command0_Click()
On Error GoTo Err_Command0_Click
' wait
If Not m_locked Then
m_locked = True
Dim startTime As Date
startTime = Now()
While DateDiff("s", startTime, Now()) < 3
DoEvents
Wend
' increment counter
m_count = m_count + 1
Command0.Caption = m_count
m_locked = False
End If
Exit_Command0_Click:
Exit Sub
Err_Command0_Click:
MsgBox Err.Description
Resume Exit_Command0_Click
End Sub
[// code]
马尔科姆
看到您的“解决方案”,我也建议您指定双击事件。这将使您可以轻松地区分用户是通过“ debug.print”单击一次还是两次。
[为了防止用户执行额外的单击,请在表单模块级别上声明一个私有布尔变量,在您的事件过程中将其设置为TRUE,在timer事件中将其设置为FALSE,然后将表单的计时器配置为1000(例如这是毫秒)。
Option Explicit
Option Compare Database
Private oneClick As Boolean
Private Sub cmdMyButton_Click()
If not oneClick Then
' Perform your actions here
End If
oneClick = True
End Sub
Private Sub Form_Timer()
oneClick = False
End Sub
哦,请使用告诉它们含义的变量和控件名称:-)
如果您不希望用户单击此按钮两次,只需此简单代码:
Private Sub Command0_Click()
Command0.Enabled = False
' Continue with your code here ...
End Sub