Close button without Saving changes

M

Maria

I am new to Access - I have Access 2003.

I have a form that contains a Close Button with code in the OnClick Event
(see below for code). However, when a person does not make a change to the
record on the form and clicks the Close button and clicks No to not save any
changes - I get an error indicating that the Undo is not understandable at
the said time.

I think I should be checking to see if a forms data has been edited - how do
I go about doing this?

Also, I do not want to place the code in the BeforeUpdate Event on the Form.

Private Sub btn_CloseEmpresas_Click()

Dim strMsg As String
strMsg = "Data has been Changed."
strMsg = strMsg & " Would you like to save changes?"
strMsg = strMsg & " Click on Yes to Save and No to Disregard the
changes."
If MsgBox(strMsg, vbQuestion + vbYesNo, "Save Data?") = vbYes Then
'do nothing
DoCmd.Close
Else
DoCmd.RunCommand acCmdUndo
DoCmd.Close

End If

End Sub


Thank you in advance
Maria
 
J

Jack Leach

You can check out the Dirty property of the form.

If Me.Dirty = True then
'Data is not saved
End If
If Me.Dirty = False Then
'Data is saved
End If

A common line of code:
If Me.Dirty Then Me.Dirty = False
(if the data isn't saved, Me.Dirty = False forces a save).


Another tip would be to use the Unload event of the form, rather than
putting it behind a command button. The Unload event will fire no matter how
the form closes... whether you have a command button onclick with
DoCmd.Close, or if the user closes the form with the X button, or even if the
exit the access application with the form still open. The only way I've come
across where this doesn't fire is on a loss of power. You can even cancel
the closing (unloading) of the form if certain criteria isn't met:

Private Sub Form_Unload(Cancel As Integer)
If Me.Dirty Then
Msgbox "Form Dirty... please save record before closing"
Cancel = True
End If
End Sub



hth

--
Jack Leach
www.tristatemachine.com

"I haven't failed, I've found ten thousand ways that don't work."
-Thomas Edison (1847-1931)
 
J

John Spencer MVP

Jack,

I would think that the form's Unload event is too late to stop changes from
being saved. I think you would have to use the form's Before Update event to
handle this.

John Spencer
Access MVP 2002-2005, 2007-2009
The Hilltop Institute
University of Maryland Baltimore County
 
J

Jack Leach

True. And I think I missed another point... Op is looking to see if the data
has been changed since the record was current... Me.Dirty doesn't help in
that case.

An alternative may be to caputure the error that Me.Undo throws if it isn't
available.



Private Sub btn_CloseEmpresas_Click()
On Error GoTo Err_Proc
Dim strMsg As String
strMsg = "Data has been Changed."
strMsg = strMsg & " Would you like to save changes?"
strMsg = strMsg & " Click on Yes to Save and No to Disregard the
changes."
If MsgBox(strMsg, vbQuestion + vbYesNo, "Save Data?") = vbYes Then
'do nothing
DoCmd.Close
Else
DoCmd.RunCommand acCmdUndo
DoCmd.Close
End If
Exit_Proc:
Exit Sub
Error_Proc:
If Err.Number = 'insert appropriate number for no undo
Err.Clear
Resume Next
Else
MsgBox Err.Number & " " & Err.Description
End If
Resume Exit_Proc
End Sub


--
Jack Leach
www.tristatemachine.com

"I haven't failed, I've found ten thousand ways that don't work."
-Thomas Edison (1847-1931)
 
M

Maria

Dear Jack and John

Thank you for ALL your help. The capturing of the error worked PERFECTLY.
My buttons save and do not save correctly.

Again, thank you for all your help
Maria
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top