【发布时间】:2017-11-27 05:32:45
【问题描述】:
我有一个关于我的班级数据库的最后一个问题。对于用户可以查看数据的某些表单,我希望他们能够关闭表单,并且如果他们没有进行任何更改,则不会收到提示。但是,如果他们选择更改表单中的某些数据,比如编辑记录,那么我希望他们得到一个弹出框,提示用户是否要保存更改。如果他们选择“是”,则旧记录将被覆盖并将更改保存到记录中,但如果他们选择“否”,则根本不会更改记录并关闭表单。
我该怎么做?我正在使用 Access 2016,到目前为止我尝试过的方法都会导致错误。以下是我在 VBA 中的 BeforeUpdate 事件表单中尝试过的两种方法。
方法一:
Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim strMsg As String
Dim iResponse As Integer
' Specify the message to display.
strMsg = "Do you wish to save the changes?" & Chr(10)
strMsg = strMsg & "Click Yes to Save or No to Discard changes."
' Display the message box.
iResponse = MsgBox(strMsg, vbQuestion + vbYesNo, "Save Record?")
' Check the user's response.
If iResponse = vbNo Then
' Undo the change.
DoCmd.RunCommand acCmdUndo
' Cancel the update.
Cancel = True
End If
End Sub
错误 - “作为事件属性设置输入的更新前的表达式产生了以下错误:Microsoft Access 与 OLE 服务器或 ActiveX 控件通信时出现问题。”
方法二:
Private Sub Form_BeforeUpdate(Cancel As Integer)
'Provide the user with the option to save/undo
'changes made to the record in the form
If MsgBox("Changes have been made to this record." _
& vbCrLf & vbCrLf & "Do you want to save these changes?" _
, vbYesNo, "Changes Made...") = vbYes Then
DoCmd.Save
Else
DoCmd.RunCommand acCmdUndo
End If
End Sub
错误 - 同上。
非常感谢任何帮助!
谢谢!
【问题讨论】: