Do not save record when leaving the form

Pumpk1n

Board Regular
Joined
Jan 31, 2017
Messages
86
Office Version
  1. 365
Platform
  1. Windows
I have a subform to which the user enters data. At the top of a form is a button which will take them to a home screen. If the user clicks on the home button, the information they have entered is saved in a new record. I would like to have some code check to see if the subform is 'dirty' and if so, provide an error message "You have unsaved data on the form - select OK to save or cancel" and have them stay on the page with focus on the 'cancel' button.

How do I write VBA for that?

thanks in advance.
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
There's a good chance that when user clicks the button, the record is saved. This is the usual behaviour of subforms when they're bound. You didn't say if either form was bound or not. If not, you cannot check the dirty property of an unbound form.
Since the button is not on the subform, you'll need the syntax for referring to a subform from the parent form:
Forms!frmName.SubformControlName.Form.Dirty
SubformControlName is the name of the control containing the subform - not the subform name. They shouldn't be the same IMHO.
The only way you can detect if a form was changed when Access is updating the record due to lost focus or similar action, is to have a module or procedure level variable that gets set to True as soon as the user changes data in a record.
 
Last edited:
Upvote 0
I took too long to edit, so here's a repost
There's a good chance that when user clicks the button, the record is saved. This is the usual behaviour of subforms when they're bound. You didn't say if either form was bound or not. If not, you cannot check the dirty property of an unbound form. If you see the edit icon (pencil) on the subform and it disappears when you click off of the form, then the record should be saved already, thus you can't undo it.
Since the button is not on the subform, you'll need the syntax for referring to a subform from the parent form:
Forms!frmName.SubformControlName.Form.Dirty
SubformControlName is the name of the control containing the subform - not the subform name. They shouldn't be the same IMHO.
The only way you can detect if a form was changed when Access is updating the record due to lost focus or similar action, is to have a module or procedure level variable that gets set to True as soon as the user changes data in a record.

The only way I know of is to provide this functionality is to copy a record to a temp table and use the variable in code as described. If the user is prompted and elects to save, you copy the record back and delete the temp. If they cancel, you simply delete the temp record. You have to design so that one user is not deleting records of another when doing this.
 
Upvote 0
I have found some information on that - You have to work with <code>Form_BeforeUpdate</code> event. Below is an example; however it does create a typical warning message: "You can't save this record at this time. Microsoft Access may have encountered an error while trying to save a record. ..."
 
Upvote 0
This may work - depends. When the form in question is a subform and you click off of that subform (e.g. give focus to a main form control) the subform BeforeUpdate event will run. This could be confusing to the user because that will happen right away. If there are main form controls with data that may or may not have been changed, they might wonder what the prompt is about if they think they're still working on a record because of the main form data. Or maybe because main form data wasn't altered.

It's a good suggestion but I think the error you got is specific to you situation. It can occur for several reasons.
Welcome to the forum, and thanks for helping!:)
 
Upvote 0
Thanks for the suggestions. I have changed to subform to a modal and it is a bound form. I have been able to incorporate the 'ok' and 'cancel' into the form so that it will either save or discard changes. I will post another follow up question on edits in the form .
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top