dbTalk Databases Forums  

Re: Not prompted for saving changes when closing dirty form

comp.databases.ms-access comp.databases.ms-access


Discuss Re: Not prompted for saving changes when closing dirty form in the comp.databases.ms-access forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Rick Brandt
 
Posts: n/a

Default Re: Not prompted for saving changes when closing dirty form - 07-14-2003 , 11:48 AM






"P" <plavallee (AT) rcn (DOT) com> wrote

Quote:
Hi. I use navigation buttons from a class I created for all my forms. For
the close button, I run the following code:

Private Sub cmdClose_Click()
DoCmd.Close acForm, frmMain.Name, acSavePrompt
End Sub

The form closes fine but I do not get a prompt for saving changes. Any
thoughts? Thank you. P
The acSavePrompt is referring to saving design changes to the form, not to data
changes which are saved by default. If you want a "Do you want to save..." prompt
you'll need to code it in the BeforeUpdate of the form.




Reply With Quote
  #2  
Old   
Allen Browne
 
Posts: n/a

Default Re: Not prompted for saving changes when closing dirty form - 07-14-2003 , 11:51 AM






The "acSavePrompt" is about saving the form (e.g. its filter property). It
has nothing to do with saving the record.

If you want to be prompted before the record is saved, you must use the
BeforeUpdate event of the *form*:

Private Sub Form_BeforeUpdate(Cancel As Integer)
Select Case MsgBox("Save changes?", vbYesNoCancel)
Case vbYes 'Do nothing: it saves by default.
Case vbNo 'Undo the changes, and close.
Cancel = True
Me.Undo
Case vbCancel 'Don't close, and don't save.
Cancel = True
end Select
End Sub

Now your code should explicitly try to save before exiting. If that fails
(as it will in the Cancel case), you need error handling:

Private Sub cmdClose_Click()
On Error Goto Err_Handler
If Me.Dirty Then
Me.Dirty = False
End If
DoCmd.Close acForm, Me.Name

Exit_Handler:
Exit Sub

Err_Handler:
Select Case Err.Number
Case 3314, 2101, 2115 'can't save: just ignore these.
Case Else
MsgBox "Error " & Err.Number & " - " & Err.Description
End Select
Resume Exit_Handler
End Sub

Note that the explicit save (setting Dirty to false) is necessary due to a
bug in Access that just silently discards your edits and closes the form
with no warning if there is any reason why the record can't be saved (e.g. a
validation rule is not meet, or a required field is not present).
--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to the newsgroup. (Email address has spurious "_SpamTrap")
"P" <plavallee (AT) rcn (DOT) com> wrote

Quote:
Hi. I use navigation buttons from a class I created for all my forms. For
the close button, I run the following code:

Private Sub cmdClose_Click()
DoCmd.Close acForm, frmMain.Name, acSavePrompt
End Sub

The form closes fine but I do not get a prompt for saving changes. Any
thoughts? Thank you. P





Reply With Quote
  #3  
Old   
P
 
Posts: n/a

Default Re: Not prompted for saving changes when closing dirty form - 07-30-2003 , 08:58 PM



Thank you Allen for your help. It works fine whenever the form is closed
either using the Close button or the x mark in upper-right corner. I also
have a Save button which runs: If frm.Dirty Then frm.Dirty = False
How can I prevent the code frm_beforeupdate() from executing when users
click on the Save button. I don't want them to be prompted for saving when
they are clicking Save.
Thank you. P

"Allen Browne" <abrowne1_SpamTrap (AT) bigpond (DOT) net.au> wrote

Quote:
The "acSavePrompt" is about saving the form (e.g. its filter property). It
has nothing to do with saving the record.

If you want to be prompted before the record is saved, you must use the
BeforeUpdate event of the *form*:

Private Sub Form_BeforeUpdate(Cancel As Integer)
Select Case MsgBox("Save changes?", vbYesNoCancel)
Case vbYes 'Do nothing: it saves by default.
Case vbNo 'Undo the changes, and close.
Cancel = True
Me.Undo
Case vbCancel 'Don't close, and don't save.
Cancel = True
end Select
End Sub

Now your code should explicitly try to save before exiting. If that fails
(as it will in the Cancel case), you need error handling:

Private Sub cmdClose_Click()
On Error Goto Err_Handler
If Me.Dirty Then
Me.Dirty = False
End If
DoCmd.Close acForm, Me.Name

Exit_Handler:
Exit Sub

Err_Handler:
Select Case Err.Number
Case 3314, 2101, 2115 'can't save: just ignore these.
Case Else
MsgBox "Error " & Err.Number & " - " & Err.Description
End Select
Resume Exit_Handler
End Sub

Note that the explicit save (setting Dirty to false) is necessary due to a
bug in Access that just silently discards your edits and closes the form
with no warning if there is any reason why the record can't be saved (e.g.
a
validation rule is not meet, or a required field is not present).
--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to the newsgroup. (Email address has spurious "_SpamTrap")
"P" <plavallee (AT) rcn (DOT) com> wrote in message
news:PZAQa.20158$BM.5816308 (AT) newssrv26 (DOT) news.prodigy.com...
Hi. I use navigation buttons from a class I created for all my forms.
For
the close button, I run the following code:

Private Sub cmdClose_Click()
DoCmd.Close acForm, frmMain.Name, acSavePrompt
End Sub

The form closes fine but I do not get a prompt for saving changes. Any
thoughts? Thank you. P







Reply With Quote
  #4  
Old   
P
 
Posts: n/a

Default Re: Not prompted for saving changes when closing dirty form - 08-05-2003 , 02:24 PM



I use this code and it works fine. However, when my form is dirty, if I
close the form using the x mark and answer No to "Save Changes?, I get error
2169: Application-defined or object-defined error. I am not sure about its
meaning. I am tempted to disregard it since the form closes fine without
saving my changes after I acknowledge the error. It seems to happen when
Me.Undo executes. Any thoughts? Thank you.


"Allen Browne" <abrowne1_SpamTrap (AT) bigpond (DOT) net.au> wrote

Quote:
The "acSavePrompt" is about saving the form (e.g. its filter property). It
has nothing to do with saving the record.

If you want to be prompted before the record is saved, you must use the
BeforeUpdate event of the *form*:

Private Sub Form_BeforeUpdate(Cancel As Integer)
Select Case MsgBox("Save changes?", vbYesNoCancel)
Case vbYes 'Do nothing: it saves by default.
Case vbNo 'Undo the changes, and close.
Cancel = True
Me.Undo
Case vbCancel 'Don't close, and don't save.
Cancel = True
end Select
End Sub

Now your code should explicitly try to save before exiting. If that fails
(as it will in the Cancel case), you need error handling:

Private Sub cmdClose_Click()
On Error Goto Err_Handler
If Me.Dirty Then
Me.Dirty = False
End If
DoCmd.Close acForm, Me.Name

Exit_Handler:
Exit Sub

Err_Handler:
Select Case Err.Number
Case 3314, 2101, 2115 'can't save: just ignore these.
Case Else
MsgBox "Error " & Err.Number & " - " & Err.Description
End Select
Resume Exit_Handler
End Sub

Note that the explicit save (setting Dirty to false) is necessary due to a
bug in Access that just silently discards your edits and closes the form
with no warning if there is any reason why the record can't be saved (e.g.
a
validation rule is not meet, or a required field is not present).
--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to the newsgroup. (Email address has spurious "_SpamTrap")
"P" <plavallee (AT) rcn (DOT) com> wrote in message
news:PZAQa.20158$BM.5816308 (AT) newssrv26 (DOT) news.prodigy.com...
Hi. I use navigation buttons from a class I created for all my forms.
For
the close button, I run the following code:

Private Sub cmdClose_Click()
DoCmd.Close acForm, frmMain.Name, acSavePrompt
End Sub

The form closes fine but I do not get a prompt for saving changes. Any
thoughts? Thank you. P







Reply With Quote
  #5  
Old   
Allen Browne
 
Posts: n/a

Default Re: Not prompted for saving changes when closing dirty form - 08-05-2003 , 08:39 PM



Error 2169 is just warning you that you cannot save the record. You can
ignore the message, but I'm not sure how you can prevent it.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to the newsgroup. (Email address has spurious "_SpamTrap")
"P" <plavallee (AT) rcn (DOT) com> wrote

Quote:
I use this code and it works fine. However, when my form is dirty, if I
close the form using the x mark and answer No to "Save Changes?, I get
error
2169: Application-defined or object-defined error. I am not sure about its
meaning. I am tempted to disregard it since the form closes fine without
saving my changes after I acknowledge the error. It seems to happen when
Me.Undo executes. Any thoughts? Thank you.


"Allen Browne" <abrowne1_SpamTrap (AT) bigpond (DOT) net.au> wrote in message
news:xcBQa.4369$wU5.3385 (AT) news-server (DOT) bigpond.net.au...
The "acSavePrompt" is about saving the form (e.g. its filter property).
It
has nothing to do with saving the record.

If you want to be prompted before the record is saved, you must use the
BeforeUpdate event of the *form*:

Private Sub Form_BeforeUpdate(Cancel As Integer)
Select Case MsgBox("Save changes?", vbYesNoCancel)
Case vbYes 'Do nothing: it saves by default.
Case vbNo 'Undo the changes, and close.
Cancel = True
Me.Undo
Case vbCancel 'Don't close, and don't save.
Cancel = True
end Select
End Sub

Now your code should explicitly try to save before exiting. If that
fails
(as it will in the Cancel case), you need error handling:

Private Sub cmdClose_Click()
On Error Goto Err_Handler
If Me.Dirty Then
Me.Dirty = False
End If
DoCmd.Close acForm, Me.Name

Exit_Handler:
Exit Sub

Err_Handler:
Select Case Err.Number
Case 3314, 2101, 2115 'can't save: just ignore these.
Case Else
MsgBox "Error " & Err.Number & " - " & Err.Description
End Select
Resume Exit_Handler
End Sub

Note that the explicit save (setting Dirty to false) is necessary due to
a
bug in Access that just silently discards your edits and closes the form
with no warning if there is any reason why the record can't be saved
(e.g.
a
validation rule is not meet, or a required field is not present).
--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to the newsgroup. (Email address has spurious "_SpamTrap")
"P" <plavallee (AT) rcn (DOT) com> wrote in message
news:PZAQa.20158$BM.5816308 (AT) newssrv26 (DOT) news.prodigy.com...
Hi. I use navigation buttons from a class I created for all my forms.
For
the close button, I run the following code:

Private Sub cmdClose_Click()
DoCmd.Close acForm, frmMain.Name, acSavePrompt
End Sub

The form closes fine but I do not get a prompt for saving changes. Any
thoughts? Thank you. P









Reply With Quote
Reply




Thread Tools
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.