Results 1 to 4 of 4
  1. #1
    MatthewR is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2015
    Posts
    118

    Cancel Update without Discarding Changes

    I have a database in which I need users to save forms via command buttons only, since I run additional code with those buttons. As such, I've hidden form close buttons.

    I've now realized that if they close the entire database with a dirty form open, that will save the form. Oops.

    I wrote code that pops up a msgbox in this situation- it allows users to close the database without saving the form, or to return to the form.

    I allow users to return to the form by inserting "Cancel=True" into the Before Update event (as well as the On Unload event).

    The problem is, when this code runs, all of their changes become undone. Is there any way to allow them to return to the form without saving the changes, but without discarding them either?

    Many thanks for your help, good people of computer land.

    Matt


    ... And in case this is helpful at all, here's the full code:

    Dim StopClose As Boolean
    Dim StopUpdate As Boolean



    Private Sub Form_BeforeUpdate(Cancel As Integer)


    Dim strMsg As String
    Dim iResponse As Integer

    StopClose = False

    If StopUpdate = True Then
    If Me.Dirty Then 'If form was changed
    ' Specify the message to display.
    strMsg = "Are you sure you want to close without saving?" & Chr(10) & Chr(10)


    strMsg = strMsg & "Click YES to Close; or NO to Return to Form."
    ' Display the message box.
    iResponse = MsgBox(strMsg, vbQuestion + vbYesNo, "Close Without Saving?")

    ' Check the user's response.
    If iResponse = vbYes Then
    DoCmd.RunCommand acCmdUndo
    Else
    Cancel = True
    StopClose = True
    End If
    End If
    End If
    End Sub




    Private Sub Form_Unload(Cancel As Integer)
    If StopClose = True Then
    Cancel = True
    StopClose = False
    End If
    End Sub

  2. #2
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    What I do is use a global variable of type Boolean. Seems like you use a Boolean, too. If the user starts a process that needs to be completed or canceled, I make the variable = True. For instance, a form's Load event would cause the variable to = True. After that, if the user tries to circumvent the Save or Cancel buttons, the Unload event checks the value of the variable and cancels if true.

    The form loads
    Code:
    gbolDontQuit = True
    Then if there is a successful save or cancel
    Code:
    gbolDontQuit = False
    The only other thing needed is the unload event.
    Code:
    If gbolDontQuit = True Then
        MsgBox "Please finish and save the record" & vbCrLf & _
               "you started to create before closing this form.", vbCritical, "Request Denied"
        Cancel = True
    End If

  3. #3
    MatthewR is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2015
    Posts
    118
    Yessssss!!! You rule, dude. Thanks so much.

  4. #4
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    Glad it worked out.

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

Similar Threads

  1. Replies: 1
    Last Post: 07-18-2015, 07:59 PM
  2. Replies: 5
    Last Post: 06-12-2015, 02:15 PM
  3. Closing a form and discarding Changes
    By lowesthertz in forum Modules
    Replies: 3
    Last Post: 03-23-2015, 03:40 PM
  4. Replies: 2
    Last Post: 03-19-2015, 01:33 PM
  5. Detecting a Before Update Cancel
    By EddieN1 in forum Programming
    Replies: 3
    Last Post: 06-09-2013, 08:04 PM

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