Solved

Cancel form close without saving data.

Posted on 2011-03-17
5
491 Views
Last Modified: 2012-05-11
I want to program a form to ask whether to save changes to a record whenever the record is new or dirty and the user attempts to move to a different record and when they attempt to close the form using any built-in Access method such as the Close button (red X).

I have the following two events in my form for this purpose:

Private Sub Form_BeforeUpdate(Cancel As Integer)
If gcfHandleErrors Then On Error GoTo Err_Form_BeforeUpdate

    Dim intSaveChanges As Integer
    intSaveChanges = MsgBox("This record has been changed. Do you want to save these changes?", vbQuestion + vbYesNoCancel, "Save Changes?")
    Select Case intSaveChanges
        Case vbYes
            Cancel = False
        Case vbNo
            DoCmd.RunCommand acCmdUndo
            Cancel = False
        Case vbCancel
            Cancel = True
    End Select

Exit_Form_BeforeUpdate:
    Exit Sub

Err_Form_BeforeUpdate:
    MsgBox "Error Number: " & Err.Number & vbCrLf & "Error Description: " & Err.Description & vbCrLf & "Error Source: " & Err.Source
    Resume Exit_Form_BeforeUpdate

End Sub

Private Sub Form_Unload(Cancel As Integer)
If gcfHandleErrors Then On Error GoTo Err_Form_Unload

    If Me.Dirty Or Me.NewRecord Then
        Dim intSaveChanges As Integer
        intSaveChanges = MsgBox("This record has been changed. Do you want to save these changes?", vbQuestion + vbYesNoCancel, "Save Changes?")
        Select Case intSaveChanges
            Case vbYes
                Cancel = False
            Case vbNo
                DoCmd.RunCommand acCmdUndo
                Cancel = False
            Case vbCancel
                Cancel = True
        End Select
    End If

Exit_Form_Unload:
    Exit Sub

Err_Form_Unload:
    MsgBox "Error Number: " & Err.Number & vbCrLf & "Error Description: " & Err.Description & vbCrLf & "Error Source: " & Err.Source
    Resume Exit_Form_Unload

End Sub

My problem is that if I attempt to close the form and click the cancel option I get the following Access message box:

 You can't save this record at this time.

MyApp may have encountered an error while trying to save a record. If you close this object now, the data changes you made will be lost. Do you want to close the database object anyway?

While I understand the message I don’t know where it is coming from or how to trap and/or disable it. It isn’t generating an error that my error trapping routines sees.
0
Comment
Question by:BrianThor
  • 3
5 Comments
 
LVL 11

Expert Comment

by:SeanStrickland
ID: 35162479
When you want to stop your form from saving data & undo changes made by the user to the form, you need two lines of code:

Cancel = True
Me.Undo

Try replacing your BeforeUpdate code with what I have below.  Hope this helps...

Private Sub Form_BeforeUpdate(Cancel As Integer)
If gcfHandleErrors Then On Error GoTo Err_Form_BeforeUpdate

    Dim intSaveChanges As Integer
    intSaveChanges = MsgBox("This record has been changed. Do you want to save these changes?", vbQuestion + vbYesNoCancel, "Save Changes?")
    Select Case intSaveChanges
        Case vbYes
            'do nothing - access will save by default
        Case vbNo
            Cancel = True
            Me.Undo
        Case vbCancel
            Cancel = True
            'I'm assuming that you want to let the user modify their values before saving.  In that case, don't put Me.Undo here because that would remove the user's changes to the form.  Cancel = True will stop the changes from hitting your recordset.
    End Select

Exit_Form_BeforeUpdate:
    Exit Sub

Err_Form_BeforeUpdate:
    MsgBox "Error Number: " & Err.Number & vbCrLf & "Error Description: " & Err.Description & vbCrLf & "Error Source: " & Err.Source
    Resume Exit_Form_BeforeUpdate

End Sub

Open in new window

0
 
LVL 11

Expert Comment

by:SeanStrickland
ID: 35162483
Same concepts should apply to your Unload event.
0
 

Author Comment

by:BrianThor
ID: 35164367
Sean,

Thanks for your comments. I probably didn't do a good job of explaining myself in the run on sentence describing my problem. The only issue I am having is that when the user cancels the form close (Form_Unload = vbCancel) they are presented with the Access generated dialog box mentioned above.  Access error dialog box
0
 

Accepted Solution

by:
deberle earned 500 total points
ID: 35235580
This is the code I use on forms in the before update event of the form (access 2003-2007)
It's real basic, If the user tries closing the form or move to a new one it asks if they want to save the
record. If no closes without saving the data. Works with new records as well as exising records that have changes.

Private Sub Form_BeforeUpdate(Cancel As Integer)
If Me.Dirty Then
    If MsgBox("CHANGES HAVE BEEN MADE TO THIS RECORD- SAVE?", vbYesNo + vbQuestion, "Save Record") = vbNo Then
            Me.Undo
     End If
   End If
   
Exit_BeforeUpdate:
    Exit Sub
   
Err_BeforeUpdate:
    MsgBox Err.Number & " " & Err.Description
    Resume Exit_BeforeUpdate
End Sub
0
 
LVL 11

Expert Comment

by:SeanStrickland
ID: 35235686
A tip I've heard in the past for this is to insert the following into your form:

Private Sub Form_Error(DataErr As Integer, Response As Integer)
Select Case DataErr
Case 2169
    Response = acDataErrContinue
Case Else
    Response = acDataErrDisplay
End Select
Debug.Print Err.Number & "   " & Err.Description
End Sub

Open in new window

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question