How to stop an edit form from saving unwanted changes?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Bigdaddrock
    New Member
    • May 2010
    • 67

    #1

    How to stop an edit form from saving unwanted changes?

    I currently have a form to allow changes to a record in a Table. Sometimes, if the user has entered some changes, but then changes their mind, is there a way that I can allow them to close the form WITHOUT making any changes to the record???
  • Stewart Ross
    Recognized Expert Moderator Specialist
    • Feb 2008
    • 2545

    #2
    Hi. You can provide a 'Close without saving changes' button on your form which undoes the current entry if changes have been made, on confirmation that abandoning the current record is what the user wants to do. If the button is named cmdAbandon, say, then the sample code below would provide a specific route out for the user:

    Code:
    Private Sub cmdAbandon_Click()
        Dim strMessage As String
        Dim blChangesMade As Boolean
        blChangesMade = Me.Dirty
        strMessage = "Are you sure you want to "
        If blChangesMade Then
            strMessage = strMessage & "abandon the changes you have made?"
        Else
            strMessage = strMessage & "exit from this form?"
        End If
        If MsgBox(strMessage, vbExclamation + vbYesNo, "Abandon this entry") = vbYes Then
            If blChangesMade Then
                Me.Undo
            End If
            DoCmd.Close
        End If
    End Sub
    This does not prevent the user from closing the form using the form's top-right close button, which would automatically save any changes made. There are ways round this, but these can be a little more complex to handle, and if you have provided a specific 'abandon' route you should really not need to go for the extra complexity of undoing the update in these circumstances.

    -Stewart

    Comment

    Working...