?
Solved

Cancel form close without saving data.

Posted on 2011-03-17
5
Medium Priority
?
520 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1500 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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
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.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

719 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