在这里快速提问,希望有一个简洁明智的解决方案。
我有一个纯粹用于数据输入的绑定表单(无法浏览记录,只能插入它们)。我会有很多用户搞砸了。为了避免脏数据,我希望他们在提交记录之前确认表格是正确的。
问题是,只要我在表单上输入任何内容,访问就会创建并保存记录。
我希望仅在用户单击“提交”时才保存和提交记录。如果他们单击关闭或退出应用程序,我不希望数据库中的部分完成记录。
不使用未绑定的表单并调用插入函数,有没有简单的解决方案?
自动编号是唯一的,而不是顺序的。如果您需要序列号,请不要使用自动编号。永远不应向用户显示自动编号。它永远不能被认为是独一无二的,如果你搞得够多,那就更不用说了。
Private Sub Form_BeforeUpdate(Cancel As Integer)
If Me.AText = "Invalid" Then
Me.Undo
Cancel = True
End If
End Sub
请注意,带有子表单的表单可能无法与撤消一起使用,因为记录是在从子表单更改为主表单时提交的,反之亦然,这一切都变得相当复杂。
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
您可以使用以下代码创建一个清除按钮,以防用户出错并希望清除整个表单并重新开始。
Private Sub btnClear_Click()
If Me.Dirty = True Then
DoCmd.RunCommand acCmdUndo
Exit Sub
End If
End Sub`
我有时会发现 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