表單的 On Open 函式設定我的變數的值,然后將其傳遞給表單計時器。單步執行它會在打開時正確格式化時間,但是一旦將其傳遞給表單計時器功能,它就會更改為日期并且無法正確顯示。
Dim timeRemaining As Date
Private Sub Form_Open(Cancel As Integer)
timeRemaining = Format(900, "Short Time")
End Sub
Private Sub Form_Timer()
timeRemaining = timeRemaining - 1
lblTimer.Caption = "You will be logged out in " & vbCrLf & timeRemaining & " second"
If timeRemaining > 1 Then
lblTimer.Caption = lblTimer.Caption & "s."
End If
DoEvents
If timeRemaining = 0 Then
'shut down
Do While Forms.Count > 0
DoCmd.Close acForm, Forms(0).Name, acSaveYes
Loop
Do While Reports.Count > 0
DoCmd.Close acReport, Forms(0).Name, acSaveYes
Loop
End If
End Sub
uj5u.com熱心網友回復:
首先,將TimerInterval屬性設定為 1000。即毫秒,即 1 秒。
接下來,像這樣修改:
Private Sub Form_Open(Cancel As Integer)
timeRemaining = TimeSerial(0, 0, 900)
End Sub
Private Sub Form_Timer()
timeRemaining = DateAdd("s", -1, timeRemaining)
lblTimer.Caption = "You will be logged out in " & vbCrLf & timeRemaining & " second"
If timeRemaining > TimeSerial(0, 0, 1) Then
lblTimer.Caption = lblTimer.Caption & "s."
End If
DoEvents
If timeRemaining <= 0 Then
'shut down
Do While Forms.Count > 0
DoCmd.Close acForm, Forms(0).Name, acSaveYes
Loop
Do While Reports.Count > 0
DoCmd.Close acReport, Forms(0).Name, acSaveYes
Loop
End If
End Sub
uj5u.com熱心網友回復:
我假設您的表單的 TimerInterval 設定為 1000 ms,因為根本不提這個。無論如何,根據您的代碼,您必須將其設定為 1000 毫秒,否則您必須timeRemaining = timeRemaining - 1相應地更改該行。
我的代碼看起來像這樣
Option Compare Database
Option Explicit
Dim timeRemaining As Long
Private Sub Form_Open(Cancel As Integer)
timeRemaining = 900
End Sub
Private Sub Form_Timer()
timeRemaining = timeRemaining - 1
lblTimer.Caption = "You will be logged out in " & vbCrLf & _
Int(timeRemaining / 60) & " minuntes " & timeRemaining - Int(timeRemaining / 60) * 60 & " seconds"
If timeRemaining > 1 Then
lblTimer.Caption = lblTimer.Caption & "s."
End If
DoEvents
If timeRemaining = 0 Then
'shut down
Do While Forms.Count > 0
DoCmd.Close acForm, Forms(0).Name, acSaveYes
Loop
Do While Reports.Count > 0
DoCmd.Close acReport, Forms(0).Name, acSaveYes
Loop
End If
End Sub
改進的代碼將在額外的行中進行計算
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/505139.html
上一篇:如何使用此VBA代碼在AccessVBA中用作公共子例程
下一篇:在Access中重新鏈接SQL表
