Results 1 to 7 of 7
  1. #1
    Subwind is offline Can Only Learn
    Windows XP Access 2010 32bit
    Join Date
    May 2012
    Location
    Portsmouth, UK
    Posts
    61

    Confirm Data Changes, Yes/No/Cancel

    I have spent a couple of days looking for a way to confirm data changes with the option of cancelling to continue editing and found nothing that worked the way I wanted.

    Finally I have managed to work a little code that does exactly what I am after.

    I have attached this to a cmdClose button on my form.

    Code:
    Private Sub cmdClose_Click()
    
        If Me.Dirty = True Then 'Check for data changes
                Select Case MsgBox("Do you want to save changes to this record?", vbYesNoCancel, "Save Changes")
                    Case vbYes 'Save changes, close form
                         DoCmd.Close
                    Case vbNo 'Undo changes, close without saving
                        Me.Undo
                        DoCmd.Close
                    Case vbCancel 'Cancel close, continue editing
                        DoCmd.CancelEvent
                End Select
        Else 'No data changes, close form
            DoCmd.Close
        End If
    
    End Sub
    Nice and simple. If you have changed the data, you get the option to save and close (Yes), undo and close (No) or continue editing (Cancel).



    Hopefully anyone else who has been searching for this without much luck can get use of it.

    ~Matt

  2. #2
    Subwind is offline Can Only Learn
    Windows XP Access 2010 32bit
    Join Date
    May 2012
    Location
    Portsmouth, UK
    Posts
    61
    This is my first post on these forums so any advice for my code would be greatly appreciated. I am self taught in Access and learn everything from what I read in these forums.

    ~Matt

  3. #3
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    Welcome aboard! One comment is that this line is probably not necessary:

    DoCmd.Save

    The Save method is saving changes to the form object, not the data. When your form is bound to a table, the data will save automatically when the form closes unless you specify otherwise, as you did with the Undo.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  4. #4
    Subwind is offline Can Only Learn
    Windows XP Access 2010 32bit
    Join Date
    May 2012
    Location
    Portsmouth, UK
    Posts
    61
    Quote Originally Posted by pbaldy View Post
    Welcome aboard! One comment is that this line is probably not necessary:

    DoCmd.Save

    The Save method is saving changes to the form object, not the data. When your form is bound to a table, the data will save automatically when the form closes unless you specify otherwise, as you did with the Undo.
    Thank you for the tip. Self taught is not always the best way. I shall remove that part from the coding.

    ~Matt

  5. #5
    Subwind is offline Can Only Learn
    Windows XP Access 2010 32bit
    Join Date
    May 2012
    Location
    Portsmouth, UK
    Posts
    61
    As a module.

    Code:
    Public Function cmdClose()
    
        If Screen.ActiveForm.Dirty = True Then 'Check for data changes
                Select Case MsgBox("Do you want to save changes to this record?", vbYesNoCancel, "Save Changes")
                    Case vbYes 'Save changes, close form
                         DoCmd.Close
                    Case vbNo 'Undo changes, close without saving
                        Screen.ActiveForm.Undo
                        DoCmd.Close
                    Case vbCancel 'Cancel close, continue editing
                        DoCmd.CancelEvent
                End Select
        Else 'No data changes, close form
            DoCmd.Close
        End If
    
    End Function
    ~Matt

  6. #6
    Subwind is offline Can Only Learn
    Windows XP Access 2010 32bit
    Join Date
    May 2012
    Location
    Portsmouth, UK
    Posts
    61
    Would anyone be able to advise the best way to add the option of a dirty subform to this code please?

    I have a few forms that have subforms in datasheet view that I would like to confirm changes on too, if this is possible.

    ~Matt

  7. #7
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    I haven't used ActiveForm much. If you call it from the subform, does ActiveForm refer to the main form or the subform? If the main form, I suspect you'd have to loop the controls of the main form and check the dirty property of any subforms.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Help to confirm if a bug (or not).
    By 245 in forum Access
    Replies: 18
    Last Post: 01-15-2012, 07:50 PM
  2. Cancel button for adding data
    By eliotchs in forum Forms
    Replies: 1
    Last Post: 11-21-2011, 04:46 PM
  3. Select Data Source Won't Cancel!
    By allenrickson in forum Reports
    Replies: 2
    Last Post: 06-21-2011, 11:55 AM
  4. Confirm Changes in a Query
    By 161 in forum Queries
    Replies: 6
    Last Post: 03-11-2011, 10:51 AM
  5. Replies: 4
    Last Post: 04-01-2009, 11:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums