PC Review


Reply
Thread Tools Rate Thread

Close button without Saving changes

 
 
Maria
Guest
Posts: n/a
 
      30th Jun 2009

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
 
Reply With Quote
 
 
 
 
Jack Leach
Guest
Posts: n/a
 
      30th Jun 2009

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

 
Reply With Quote
 
John Spencer MVP
Guest
Posts: n/a
 
      30th Jun 2009

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

Jack Leach wrote:
> 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
>

 
Reply With Quote
 
Jack Leach
Guest
Posts: n/a
 
      30th Jun 2009

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)



"John Spencer MVP" wrote:

> 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
>
> Jack Leach wrote:
> > 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
> >

>

 
Reply With Quote
 
Maria
Guest
Posts: n/a
 
      1st Jul 2009

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

"Jack Leach" wrote:

> 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)
>
>
>
> "John Spencer MVP" wrote:
>
> > 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
> >
> > Jack Leach wrote:
> > > 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
> > >

> >

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How can I make prevent the Validate event from going off when the user hits the Close button (the X button) or selects "Close" from the system menu? 0to60 Microsoft Dot NET Framework Forms 5 10th Sep 2008 10:46 AM
Disable Close button and remove Close menu and then reactivate fpsoft Microsoft Dot NET Framework Forms 0 23rd Sep 2007 03:55 PM
Excel shoud not close all active books when clicking close button =?Utf-8?B?dGVjaG5vbWlrZQ==?= Microsoft Excel Misc 0 10th Jun 2005 05:35 PM
excel - Windows close button (x) should only close active workboo. =?Utf-8?B?Q29mZmVlQWRpY3Q=?= Microsoft Excel Setup 3 8th Feb 2005 04:30 AM
Button to close without saving Newbie1 Microsoft Excel Programming 4 12th Feb 2004 10:31 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:36 PM.