Save Button versus Save safety net (1 Viewer)

monkeytunes

Serf of the Jungle
Local time
Today, 06:55
Joined
Jun 8, 2004
Messages
120
Because my users like the standard safety net of "Do you wish to save these changes?", I used some code from this post in the Before Update event of many of my forms.

However, many of the users, being proactive, conscientious sorts, like to Save on their terms. So I put in a command button using the wizard for saving records.

When the button is pressed, it immediately pops up the window that says "Do you wish to save these changes?", instead of merely saving the record. Is there a workaround so that when the button is pressed, the record is saved, but if the form is closed when the record is dirtied, the warning pops up?
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 08:55
Joined
Feb 28, 2001
Messages
27,175
You'll have to look up the event firing order, but basically if the form is dirty, you have to cancel the attempt to close the form. Find whatever event in the "close" chain has a Cancel option. In that one, test Me.Dirty (which is TRUE if any record has been modified but not saved.) Put up a message box and cancel the closure event. (Or intervene and force a save with a DoCmd operation matching the code underlying your actual SAVE button.)
 

monkeytunes

Serf of the Jungle
Local time
Today, 06:55
Joined
Jun 8, 2004
Messages
120
Thanks for the quick reply Doc Man, but I'm really terrible with working out VB code on my own. I can usually (okay, a little less than "usually") decipher what's happening in pre-existing code, but if you've got a moment, I'm going to need more guidance with the advice you gave me.

Because this line -

Private Sub Form_BeforeUpdate(Cancel As Integer)
If MsgBox("Do you want to save the changes?", vbQuestion + vbYesNo, "Save?") = vbNo Then Cancel = True
End Sub

- is in the Before Update event of the Form, pressing the "Save Record" command button triggers it. Usually this Before Update works great, because a user might try to close the form or view a new record, and Access will jump and say "WAIT! Save?", but I want a users' deliberate pressing of the Save button to bypass the Before Update code.

I'm afraid users will find it annoying to press Save Record, and then get the message box asking "Do you wish to save?" I know I'd be answering back: "Of course I want to save! I just pressed the button, didn't I?!"
 

ghudson

Registered User.
Local time
Today, 09:55
Joined
Jun 8, 2002
Messages
6,195
You are almost stuck in a loop. You might benefit from my A Better Mouse Trap? sample for that is my preferred way to ensure that a user has saved a modified record before they can move to another record or close the form. I created the method to prevent mouse wheel movement but it also keeps the user out of trouble to correctly save when needed.
 

monkeytunes

Serf of the Jungle
Local time
Today, 06:55
Joined
Jun 8, 2004
Messages
120
Ghudson, thanks for the tip. I've actually looked at that DB before (since it was rated so highly, and with good reason), but I never thought of modifiying the code for this sort of thing...

But let me ask: Is the process really that complicated? I thought the functionality I was looking for was actually pretty standard - look at MS Word, or Excel, or pretty much any program out there:
  • You have a document, you change it, you save it, you close it without any hangups.
  • You have a document, you change it, you try to close it, the program says "You want to save before closing?"

Hell, Access already does that if you modify a form, and then try to close it without saving. I figure this functionality must be available somewhere...
 

ghudson

Registered User.
Local time
Today, 09:55
Joined
Jun 8, 2002
Messages
6,195
You can not compare Excel or Word to Access. Access is a different beast.

The code you are using in the before update event is triggered when you click your custom save button because the record is dirty. You have to take the extra steps to account for that.

The method in my better mouse trap sample will allow you to do what you want. It is not hard to add my method to your db once you understand how it works. If there is a better way I would be interested to see it.
 

monkeytunes

Serf of the Jungle
Local time
Today, 06:55
Joined
Jun 8, 2004
Messages
120
Holy smokes, Rich, I think you've done it. Will this code function in 2000/2002 as is?
 
R

Rich

Guest
I don't see why not, I don't have those versions to test it though
 

Users who are viewing this thread

Top Bottom