如果我沒有填寫表格中的所有必填字段,當點擊右上角的「x」時會出現一個難看的錯誤。 我想用自定義錯誤消息來覆蓋默認錯誤,但我無法弄清楚哪個VBA事件將此代碼關聯。當我把一個錯誤處理程序放在那裏時,Form_Close事件似乎不起作用。關閉表單時會觸發什麼VBA事件?
訪問2010
Private Sub Form_Unload(Cancel As Integer)
On Error GoTo Err_Unload ' Initialize error handling.
'insert routine
Exit_Unload: ' Label to resume after error.
Exit Sub ' Exit before error handler.
Err_Unload: ' Label to jump to on error.
'MsgBox Err & " " & Error$ ' Place error handling here.
Resume Exit_Unload
End Subenter code here
即使在使用此代碼仍然會收到錯誤。
Private Sub Form_Unload(Cancel As Integer)
On Error GoTo Err_Unload ' Initialize error handling.
' Code to do something here.
Exit_Unload: ' Label to resume after error.
Exit Sub ' Exit before error handler.
Err_Unload: ' Label to jump to on error.
'MsgBox Err & " " & Error$ ' Place error handling here.
Resume Exit_Unload
End Sub
如果您未填寫必填字段並嘗試關閉表單,則會在觸發任何表單事件(2007年確認)之前顯示醜陋的錯誤消息。爲了避免這種情況,您需要爲您的必填字段設置默認值或使用On Current事件來確定它們。只要確保在更新前的事件中檢查記錄以確保輸入值是可接受的。 –