我有一个表单,用于查询用户可能想要编辑的记录。我希望用户只有在点击了“保存”字样后才能保存记录。按钮。点击关闭'如果用户尚未保存,则按钮将提示用户,并可能询问他们是否要保存。
我在用户更改记录时遇到问题:我希望有一个Y / N消息框提示用户保存他们对上一条记录所做的更改,否则他们的更改将被丢弃。我有以下代码设置:
Private Sub CmdCloseForm_Click()
If Me.Dirty Then
'checks that needed fields are completed
If IsFormValidated = False Then
If MsgBox("Required fields aren't filled." & vbCrLf & "Would you like to close this form without saving?", vbYesNo + vbQuestion + vbDefaultButton2, "Warning") = vbNo Then
Exit Sub
End If
Else
'checks if form has been saved already
If mSaved = False Then
Select Case MsgBox("Form hasn't been saved. Do you want to save and close?" & vbCrLf & "If you click 'No' the form will close without saving.", vbQuestion + vbYesNoCancel, "Save As")
'selecting yes will save and close form
Case vbYes:
mSaved = True
'selecting no will close the form w/o saving
Case vbNo:
mSaved = False
'selecting cancel will cancel out of the prompt
Case vbCancel:
Exit Sub
End Select
ElseIf mSaved = True Then
'if form has been previously saved, will finally close the form
If MsgBox("Would you like to close this form?", vbYesNo + vbQuestion + vbDefaultButton2, "Close Form") = vbNo Then
Exit Sub
End If
End If
End If
End If
DoCmd.Close acForm, Me.Name, acSaveNo
End Sub
'won't save automatically unless mSaved is true
Private Sub Form_BeforeUpdate(Cancel As Integer)
'if mSaved = False then the record won't save
If mSaved = False Then
Cancel = True
Me.Undo
Cancel = False
End If
End Sub
我非常想要' CmdCloseForm'消息框在用户移动到下一条记录时运行。有没有办法做到这一点?
答案 0 :(得分:1)
如果您确实想要询问用户是否保存每行的更改,请在Form_BeforeUpdate
和Form_BeforeDelConfirm
中询问。每次用户更改编辑记录时,或者当具有已编辑记录的子窗体失去焦点,或者用户使用数据关闭窗体时,将触发这些事件。但这不是一个好的解决方案,因为消息框太烦人了。更好的方法是将编辑数据复制到临时表,允许用户编辑数据,并在用户单击“保存”时复制回源表更改的数据。如果您不需要多用户数据编辑,这很简单,在这种情况下,您需要一些额外的代码来避免冲突。