vba 关闭绑定表单而不保存更改
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14146493/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Closing a bound form without saving changes
提问by Scotch
Quick question here, hoping for a concise sensible solution.
在这里快速提问,希望有一个简洁合理的解决方案。
I have a bound form purely for data entry (cannot browse records, only insert them). I will have a lot of users that screw up. To avoid dirty data, I want them to confirm that the form is correct before submitting the record.
我有一个纯粹用于数据输入的绑定表单(无法浏览记录,只能插入它们)。我会有很多用户搞砸了。为了避免脏数据,我希望他们在提交记录之前确认表单是正确的。
Problem is, as soon as I input ANYTHING on the form, access creates and saves a record.
问题是,只要我在表单上输入任何内容,访问就会创建并保存记录。
I want the record to ONLY be saved and committed if the user clicks 'Submit'. If they click close or exit the application, I do not want that partially completed record in the database.
如果用户单击“提交”,我希望仅保存和提交记录。如果他们单击关闭或退出应用程序,我不希望数据库中的部分完成记录。
Without using an unbound form and calling an insert function, is there a simple solution?
不使用未绑定的表单并调用插入函数,是否有简单的解决方案?
回答by Fionnuala
An autonumber is there to be unique, not sequential. If you need a sequential number, do not use autonumber. Autonumber should never be shown to the user. It can never be relied upon to be anything but unique, and if you mess about enough, not even that.
自动编号是唯一的,而不是连续的。如果您需要序列号,请不要使用自动编号。永远不应向用户显示自动编号。它绝不能依赖于独一无二的东西,如果你搞得够多了,甚至连那个都不行。
Private Sub Form_BeforeUpdate(Cancel As Integer)
If Me.AText = "Invalid" Then
Me.Undo
Cancel = True
End If
End Sub
Note that a form with a subform may not work with undo, because the record is committed on change from the subform to the main form and vice versa and it all gets quite complicated.
请注意,带有子表单的表单可能无法使用撤消,因为记录是在从子表单更改为主表单时提交的,反之亦然,这一切都变得非常复杂。
回答by bonCodigo
Remou's method is definitely the quickest, here is another based on my comment ;)
Remou 的方法绝对是最快的,这是另一个基于我的评论的方法 ;)
Option Explicit
Private blnGood As Boolean
Private Sub cmdSubmit_Click()
blnGood = True
Call DoCmd.RunCommand(acCmdSaveRecord)
blnGood = False
End Sub
Private Sub Form_BeforeUpdate(Cancel As Integer)
If Not blnGood Then
Cancel = True
Call MsgBox(Prompt:="click submit to save the record", Title:="Before Update")
End If
End Sub
回答by Kefash
You can use the following code to create a clear button in case the user made an errors and wants to clear the entire form and start over.
您可以使用以下代码创建一个清除按钮,以防用户出错并希望清除整个表单并重新开始。
Private Sub btnClear_Click()
If Me.Dirty = True Then
DoCmd.RunCommand acCmdUndo
Exit Sub
End If
End Sub`
I sometimes find the before_update event to act weird so I generally disable the close (x) button in properties and add a close button of my own that prompts the user if he wants to abandon the data on screen.
我有时会发现 before_update 事件的行为很奇怪,所以我通常禁用属性中的关闭 (x) 按钮并添加我自己的关闭按钮,提示用户是否要放弃屏幕上的数据。
Private Sub close_Click()
Dim Answer As Integer
If Me.Dirty = True Then
Dim Response As Integer
' Displays a message box with the yes and no options.
Response = MsgBox(Prompt:="Do you wish to discard data?", Buttons:=vbYesNo)
' If statement to check if the yes button was selected.
If Response = vbYes Then
DoCmd.RunCommand acCmdUndo
DoCmd.Close
Else
Me.Clear.SetFocus
End If
Else
' The no button was selected.
DoCmd.Close
End If
End Sub