Preventing close buttons from saving records in MS Access

后端 未结 6 1196
小蘑菇
小蘑菇 2021-02-10 11:42

In a Microsoft Access form, whenever the current record changes, any changes in bound controls are silently saved to the database tables. This is fine, but I don\'t want it to h

6条回答
  •  生来不讨喜
    2021-02-10 12:18

    this is code I have that checks to see if the form is being closed or saved.

    Private Sub Form_BeforeUpdate(Cancel As Integer)
    
    If Not UsingSaveButton Then
        If MsgBox("Abandon Data?", vbInformation + vbYesNo) = vbNo Then
            Cancel = True
        Else
            DoCmd.RunCommand acCmdUndo
        End If
    End If
    End Sub
    

    I have a Boolean Flag that is set to False on loading, and then when my Save button is used, I set it to true to allow the update to run through.
    If the flag is not set, then they are leaving the record (either through going to a different record, or closing the form) so I ask if they actually want to save the changes.
    The Cancel = True aborts the exit of the form or the move to a different record if any changes have been made.
    The DoCmd.RunCommand acCmdUndo undoes any changes so they are not saved.

提交回复
热议问题