简体   繁体   English

单击进入子窗体时自动保存 MS Access 记录

[英]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.

相关问题 使用变量作为 MS Access 子表单记录源的 SQL Server 查询 - SQL Server Query with variables as MS Access subform record source MS Access-基于相同表但记录不同的表单和子表单 - MS Access - Form and subform based on same table, but different record 查询不会更新子表单 MS-Access 中的表 - Query will not update table in subform MS-Access 记录在 SQL Server 中排序,但是当带到 MS Access 子窗体时,排序不同 - Records are sorted in SQL Server, but when brought to MS Access subform, the sorting is different 导航到新字段时不保存子表单的访问子表单 - Access Subform of Subform Not Saving When Navigating to New Fields MS ACCESS 中的 IF EXISTS 记录 - IF EXISTS record in MS ACCESS MS Access子窗体-ODBC调用失败,无法在VBA中更改“ LinkFields” - MS Access SubForm - ODBC Call Failed changing “LinkFields” in VBA 为什么我的子表单在MS Access 2007的INS​​ERT操作中不包含来自父表单的外键? - Why is my subform not including the Foreign Key from the parent form in the INSERT operation in MS Access 2007? 当MS Access中我的表单的记录源在SQL Server上时,视图不可更新 - When record source of my form in MS Access is on SQL Server, view is not updatable MS Access:导入表并保留现有记录 - MS Access: Importing Table with keeping the existing record
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM