每當我用腳本命中錯誤時,焦點轉向VBA代碼和違規行。我修復它,並保存。然後我發現腳本不再運行,即使在我確定它沒有暫停之後。爲什麼調試後我的VBA腳本不能繼續運行?
例如,我現在使用Form_Timer()
事件來做一些測試(間隔設置爲1000ms)。爲了再次測試腳本,我將它設置爲將來的一分鐘(例如,如果當前時間是上午8:54:00,我將它設置爲在上午8:55:00啓動)。但是在錯誤發生後這會停止工作。有人知道爲什麼嗎?我不想告訴我的用戶關閉並重新打開Access數據庫的副本,只是爲了讓腳本再次運行。
代碼:
Private Sub Form_Timer()
On Error GoTo ErrorHandler
current_date_time = Now
If current_date_time = #6/28/2016 8:52:00 AM# Then
MsgBox ("the current_date_time variable holds: " & current_date_time)
'Declare objects
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim qdf As DAO.QueryDef
Dim oApp As Outlook.Application
Dim oMail As Outlook.MailItem
Dim mail_body As String
'Set objects
Set dbs = CurrentDb
Set qdf = dbs.QueryDefs("qry_BMBFLoc")
Set rst = qdf.OpenRecordset
Set oApp = New Outlook.Application
Set oMail = oApp.CreateItem(olMailItem)
mail_body = "The following jobs do not have the special BF location set in Job Orders: " & vbCrLf
If Not (rst.EOF And rst.BOF) Then
rst.MoveFirst
Do Until rst.EOF = True
mail_body = mail_body & rst!job & "-" & rst!suffix & vbCrLf
rst.MoveNext
Loop
'Email contents
oMail.Body = mail_body
oMail.Subject = "Blah"
oMail.To = "[email protected]"
oMail.Send
'Close stuff
rst.Close
dbs.Close
Set rst = Nothing
Set oMail = Nothing
Set oApp = Nothing
End If
End If
Exit Sub
ErrorHandler:
Dim msg As String
If Err.Number <> 0 Then
msg = "email Form Timer Error #" & Str(Err.Number) & " error Line: " & Erl & Chr(13) & Err.Description
MsgBox msg, , "Error", Err.HelpFile, Err.HelpContext
End If
Exit Sub
End Sub
這可能是一個愚蠢的問題,但在你調試你的宏後,你打f5繼續腳本? – jellz77
@ jellz77不,我沒有。在我打F5後,它彈出一個對話框,要求輸入一個宏名稱,但我不知道要給它一個宏。我正在運行VBA腳本... – whatwhatwhat
我調試後,確保腳本沒有按下Ctrl + S(保存)來暫停。它刪除頂部的'[break]',所以我認爲腳本恢復了。但它不再發射。 – whatwhatwhat