访问AutoExec for DetectIdleTime会破坏其他代码

时间:2016-12-14 23:44:38

标签: vba ms-access macros

我有一个使用Access 2007-2010的数据库,我有一个AutoExec,自动开始检测用户何时以及如果用户离开前端,它将在一小时后关闭。我使用了Microsoft网站https://msdn.microsoft.com/en-us/library/office/ff192885.aspx的“检测用户空闲时间”,最后我将其保存并关闭了程序,而不是他们是如何做到的。我遇到的问题是它打破了用vba编写的所有其他代码。我有另一个带有按钮的表单,可以复制在表中的内容并更改用户想要的内容,并且所有这些按钮都有效,直到我添加了这个宏。当我注释掉我的所有代码以便它什么都不做而我将宏保持相同以启动时,它仍然会破坏其他代码。我不确定是不是因为我有一个宏启动一个表单,其中包含代码来检测持续运行的空闲时间,导致其他所有部分工作。

My Macro that is named "AutoExec"

这是我检测空闲时间的代码

    Option Compare Database

    Sub Form_Timer()

     ' IDLEMINUTES determines how much idle time to wait for before
     ' running the IdleTimeDetected subroutine.
     Const IDLEMINUTES = 60

     Static PrevControlName As String
     Static PrevFormName As String
     Static ExpiredTime

     Dim ActiveFormName As String
     Dim ActiveControlName As String
     Dim ExpiredMinutes

     On Error Resume Next

     ' Get the active form and control name.

     ActiveFormName = Screen.ActiveForm.Name
     If Err Then
        ActiveFormName = "No Active Form"
        Err = 0
     End If

     ActiveControlName = Screen.ActiveControl.Name
        If Err Then
        ActiveControlName = "No Active Control"
        Err = 0
     End If

    Me.CurFormtxt = ActiveFormName

     ' Record the current active names and reset ExpiredTime if:
     '    1. They have not been recorded yet (code is running
     '       for the first time).
     '    2. The previous names are different than the current ones
     '       (the user has done something different during the timer
     '        interval).
     If (PrevControlName = "") Or (PrevFormName = "") _
       Or (ActiveFormName <> PrevFormName) _
       Or (ActiveControlName <> PrevControlName) Then
        PrevControlName = ActiveControlName
        PrevFormName = ActiveFormName
        ExpiredTime = 0
     Else
        ' ...otherwise the user was idle during the time interval, so
        ' increment the total expired time.
        ExpiredTime = ExpiredTime + Me.TimerInterval
     End If

     ' Does the total expired time exceed the IDLEMINUTES?
     ExpiredMinutes = (ExpiredTime / 1000) / 60
     Me.TimeInactivitytxt = ExpiredMinutes
     If ExpiredMinutes >= IDLEMINUTES Then
        ' ...if so, then reset the expired time to zero...
        ExpiredTime = 0

        ' ...and close the program
        Application.Quit acQuitSaveAll
     End If
  End Sub

2 个答案:

答案 0 :(得分:1)

在搞乱之后,我找到了解决这个问题的方法。我所做的是在下面找到了一个新方法,我用其他方法两次调用它。这样它首先关闭计时器,完成它必须做的事情并再次调用它来重新启用计时器。唯一不好的是我必须将它添加到需要它的所有方法中。

Private Sub TimerIntervalSwitch()

'MsgBox "TimerIntervalSwitch was called"
'To turn off and on the timer on the form DetectIdleTime
If Form_DetectIdleTime.TimerInterval = 1000 Then
    Form_DetectIdleTime.TimerInterval = 0
    MsgBox "Timer Off"
ElseIf (Form_DetectIdleTime.TimerInterval = 0) Then
    Form_DetectIdleTime.TimerInterval = 1000
    MsgBox "Timer On"

End If
End Sub

答案 1 :(得分:0)

这是一个众所周知的问题。计时器中断并可能破坏用户当前正在使用的内容。

可悲的是,没有办法绕过它,多年来我一直想知道为什么仍然推荐这样的代码。它只会引起麻烦 - 正如你所看到的那样。