gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 07:40:23 25 4
gpt4 key购买 nike

在我的表单“别名”的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,该错误2501在表单的更新前事件取消DoCmd.RunCommand acCmdSaveRecord时触发

下面的示例来自我的表单,该表单具有用于保存当前记录的命令按钮。如我所愿,它禁止显示“RunCommand操作被取消”消息。

由于您的表单的代码显然是从多个位置调用DoCmd.RunCommand acCmdSaveRecord,因此像在cmdSave_Click()中一样,将每个调用替换为SaveRecord

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com