View Full Version : Save or Cancel Prompt


Fiona
10-27-2000, 04:28 AM
I Expect there is a really simple answer but I am quite new to this so please bear with me. On one of my forms I have put in code that prompts the user to save or cancel the changes on closing. I am using a simple If Me.Dirty statement and I have put it in the On Click property of the Close button. However this only works when the record is closing - how do I get the same code to work when the user is clicking through from one record to another making changes without closing the form?
I am pretty much self taught - what glaringly obvious thing am I missing?
Thanks in anticipation,

Fiona

R. Hicks
10-27-2000, 04:47 AM
Call the On Click prodedure of the button from the On Current event of your form:

Private Sub Form_Current()
Call cmdYourCloseButton_Click
End Sub

Change "cmdYourCloseButton" to the name of you Close button.

This will cause the code attached to the Close Button to run when you change records.

HTH
RDH

[This message has been edited by R. Hicks (edited 10-27-2000).]

Pat Hartman
10-27-2000, 09:17 AM
The On Current event is too late to help in this situation. The On Current event runs when a new record is presented. That is after the previous record has been saved if it was updated. You need to move your code from the OnClick event of the close button to the BeforeUpdate event of the form. The BeforeUpdate event of the form is the last event executed before a record is saved. This event is executed regardless of what prompted the save operation and regardless of whether the record is being added or changed. You do not need to test if the record is dirty since this event is only executed when the dirty flag is true.

R. Hicks
10-27-2000, 01:31 PM
Pat, I stand corrected. I should have thought the process through before replying.

You know, your knowledge of Access scares me to death sometimes!!!!!!

Thanks for correcting me,
RDH

Fiona
10-30-2000, 05:52 AM
Thanks Pat,
I have moved the code to the before update bit and it works really well. However, there are save and cancel buttons on the form as well and if the user clicks these they get the message box asking them to save the record. Is ther any way I can avoid this?
Thanks again,

Fiona

Pat Hartman
10-30-2000, 01:07 PM
Add a hidden unbound control to the form. Put a value there if the user presses either the save or cancel button. Then in the form's BeforeUpdate event check this unbound field. If it has a value, you can bypass the prompt.

The result of this is if your user follows the your plan for exiting the form, he will not be prompted. If he does something else, he will be prompted.