Solved

Cancel form close without saving data.

Posted on 2011-03-17
5
471 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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

In the article entitled Working with Objects – Part 1 (http://www.experts-exchange.com/Microsoft/Development/MS_Access/A_4942-Working-with-Objects-Part-1.html), you learned the basics of working with objects, properties, methods, and events. In Work…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

757 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now