Solved

Cancel form close without saving data.

Posted on 2011-03-17
5
484 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…

856 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