[英]MS Access record automatically saved when click into subform
I have a typical Order data entry form/subform setup in MS Access / SQL Server 2019. There are 2 buttons within the form: Save / Cancel.我在 MS Access/SQL Server 2019 中有一个典型的订单数据输入表单/子表单设置。表单中有 2 个按钮:保存/取消。
This is the code for the buttons:这是按钮的代码:
Private Sub cmdSave_Click()
On Error Resume Next
DoCmd.Close acForm, "frmDEPurchaseOrder", acSaveYes
RefreshForm ("frmPurchaseOrderDetails")
RequeryForm ("frmPurchaseOrderList")
End Sub
Private Sub cmdCancel_Click()
On Error Resume Next
If MsgBox("Změny budou zrušeny. Chcete pokračovat?", vbYesNoCancel + vbExclamation, "Upozornění") <> vbYes Then Exit Sub
Me.Undo
DoCmd.Close acForm, "frmDEPurchaseOrder", acSaveYes
RefreshForm ("frmPurchaseOrderDetails")
RequeryForm ("frmPurchaseOrderList")
End Sub
"Save" button works as expected, however when I'm adding a new Order, fill the basic order information and then click into the subform to add items, a record for the order is created at this moment in time. “保存”按钮按预期工作,但是当我添加新订单时,填写基本订单信息,然后单击子表单添加项目,此时会及时创建订单记录。
The problem is that when I then click on "Cancel", the order is still saved in SQL Server 2019.问题是当我点击“取消”时,订单仍然保存在 SQL Server 2019 中。
Is there any simple way to not save / delete the record if I click cancel at any time during this process?如果在此过程中随时单击取消,是否有任何简单的方法可以不保存/删除记录?
As long as you are working with bound forms a record will be created as soon as your input focus moves from that record to something else.只要您使用绑定表单,只要您的输入焦点从该记录移动到其他内容,就会创建一个记录。 You have two options.
你有两个选择。 The clean one is to create new orders on separate tables and only transfer them to the main tables on "Save".
干净的一种是在单独的表上创建新订单,并且仅在“保存”时将它们转移到主表。 The easier but dirtier one is to clean up by deleting any created records on "Cancel".
更简单但更脏的方法是通过删除“取消”上创建的任何记录来进行清理。 Either way you are going to have to manage the records with your code, you will not be able to get away with just a parameter on how you close the form.
无论哪种方式,您都必须使用代码管理记录,您将无法仅使用有关如何关闭表单的参数。
If your order items sub-table are set up to cascade delete when the entry on the main order table is deleted it is relatively easy to go with option two.如果您的订单项目子表设置为在删除主订单表上的条目时级联删除,则使用选项二相对容易。
On the Subform GetFoucs event you can use the following code在 Subform GetFoucs 事件中,您可以使用以下代码
Sub YourSubForm_GetFoucs()
Me.Parent.Refresh
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.