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)
"Maria" wrote:
> 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