2016-12-14 75 views
1

我有一個使用Access 2007-2010的數據庫,我有一個AutoExec,自動啓動檢測何時以及如果用戶離開前端打開,它會在一小時後關閉。我使用了微軟網站https://msdn.microsoft.com/en-us/library/office/ff192885.aspx的檢測用戶空閒時間,並在結束時我保存並關閉了程序,而不是他們是如何做到的。我遇到的問題是它打破了用vba編寫的所有其他代碼。我有另一種形式的按鈕,它們複製表格中的所有東西,並改變用戶想要的內容,並且所有這些都可以工作,直到我添加了這個宏。當我將所有的代碼註釋掉以至於它什麼也不做,並且讓宏啓動時它仍然會破壞其他代碼。林不知道是否因爲我有宏啓動一個窗體與代碼來檢測空閒時間,不斷運行,導致其他部分工作。訪問DetectIdleTime AutoExec打破其他代碼

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 
+0

什麼是隱藏窗體的TimerInterval設置?我必須說,我使用'Form_Timer'做了幾件事,它從來沒有破過任何東西。 – Andre

+0

TimerInterval用於指定表單上定時器事件之間的時間間隔(以毫秒爲單位)。我從MS網站上得到了這個,只是改變了底部,關閉了數據庫,而不是他們所做的。你也可以發佈你的Form_Timer,我想看看它。 – Kayracer

+0

對不起,我知道'TimerInterval'是什麼。 :)我的意思是:你的表單有哪些TimerInterval值?即您的子女多久召喚一次? – Andre

回答

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 
0

這是一個衆所周知的問題代碼。計時器中斷並可能破壞用戶當前正在處理的內容。

不幸的是,它沒有辦法繞過它,並且我想了多年來爲什麼仍推薦這樣的代碼。如你所見,它只會造成麻煩。

+0

這很令人沮喪。我添加這個的唯一原因是因爲我需要對數據庫進行壓縮和修復,而且我似乎無法讓每一個人都失敗,或者它是腐敗的,並且認爲當沒有人時,每個人都開着。我將嘗試複製數據庫,壓縮並修復它,併發送一個連接到新後端的新前端。這是我想到的最好的 – Kayracer