ms-access - 如何在 "The runCommand action was canceled"语句后删除讨厌的 "cancel = true"对话框

标签 ms-access error-handling vba

在我的表单“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/

相关文章:

c# - 如何使用设计器生成的数据表手动设置自动增量列的 ID?

python - Python : magic squares bruteforce and errors [duplicate]

.net - WCF 错误处理和访问类对象

javascript - 如何使用 VBA 在 IE 上使用 Java 类型按钮

c++ - C++ ATL OleAutomation 错误 424

windows - 如何隐藏 MS Access 父 MDI 窗口,但仍然显示 Windows 任务栏引用?

php - 错误: could not find driver - Using PDO with MS Access database

python - 如何解决 “name '机器人“未定义”错误?

vba - 如何在VBA中保存工作表

vba - 在 MS Access 中附加链接表时出现运行时错误