在我的表单“Alias”的 Form_beforeupdate() 事件中,我有这个...
If IsNull(Me.txtFName) And IsNull(Me.txtLName) Then
MsgBox "You must enter a contact!", vbokayonly, "Contact"
Cancel = True
End If
任何时候这都会取消 runCommand 代码,例如...
DoCmd.RunCommand acCmdSaveRecord
出现一个对话框,告诉我它已被取消。有没有办法抑制这些对话框?
最佳答案
您可以添加错误处理程序来捕获并忽略错误 2501,该错误在表单的 Before Update 事件取消时触发 DoCmd.RunCommand acCmdSaveRecord
以下示例来 self 的表单,该表单有一个用于保存当前记录的命令按钮。正如我认为您希望的那样,它会抑制“RunCommand 操作已取消”消息。
因为您的表单代码显然调用 DoCmd.RunCommand acCmdSaveRecord
从多个位置,将每个调用替换为 SaveRecord
正如我在cmdSave_Click()中所做的那样。
Option Compare Database
Option Explicit ' <-- NEVER troubleshoot VBA code without this!!!
Private Sub cmdSave_Click()
'DoCmd.RunCommand acCmdSaveRecord
SaveRecord
End Sub
Private Sub SaveRecord()
Dim strMsg As String
On Error GoTo ErrorHandler
DoCmd.RunCommand acCmdSaveRecord
ExitHere:
Exit Sub
ErrorHandler:
Select Case Err.Number
Case 2501 ' The RunCommand action was canceled.
' do nothing --> just ignore the error
Case Else
' notify user about any other error
strMsg = "Error " & Err.Number & " (" & Err.Description _
& ") in procedure SaveRecord"
MsgBox strMsg
End Select
Resume ExitHere
End Sub
Private Sub Form_BeforeUpdate(Cancel As Integer)
If IsNull(Me.txtBlock_start.Value) Then
MsgBox "You must enter a start time!", vbOKOnly, "Start Time"
Cancel = True
End If
End Sub
关于ms-access - 如何在 "The runCommand action was canceled"语句后删除讨厌的 "cancel = true"对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30201932/