Use which Form event to prompt user to save changes?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a form and subform that I would like to prompt the user to save
changes when moving from record to record or closing the form. In which Form
and/or Subform event should I place my code?

Arlene
 
swedbera said:
I have a form and subform that I would like to prompt the user to save
changes when moving from record to record or closing the form. In which Form
and/or Subform event should I place my code?

Changes are automatically saved. Did you mean to say that you want to
provide an option NOT to save? If so that would normally go in the
BeforeUpdate event, but it won't work if your form has a subform because
changes are automatically saved when you move between the main form and the
subform.
 
It is quite easy to do what you ask if you do NOT use sub-forms.

however, ms-access is designed to save your data, and does so automatically
for you. ms-access is NOT at all like a word document, or a spreadsheet
where you work on a document..and then decide to save your work.


if you use a form, and a sub-form, then the instant your cursor or you click
on a control in the sub-form, the main data form is SAVED to disk for you.
The reason for this is that in a one to many relationship, you MUST first
add the parent record before you attempt to add child records to the child
table (in this case, the child table obviously the records in the sub-form).
So, the instant you move into the sub-form to add records, access will NOT
know which records these child records belong to unless the parent record is
saved. Thus, trying to place a "save" button on the form is really not much
of use if you have sub-forms.

However, if you do want to prompt the user, then the event you are looking
for is the BEFORE UPDATE event. You need place the following code in both
the forms before update..and also in the sub-forms before update....


Private Sub Form_BeforeUpdate(Cancel As Integer)

If MsgBox("Do you want to save ?", vbQuestion + vbYesNo) <> vbYes Then
Me.Undo
End If

end Sub
 
Could I add the same code to both the form and the subform? Will it also
cancel the navigation to another record when I add this to the BeforeUpdate
event, and if so, how to I move to another record once the user has answered
"no" when prompted to save record changes?

Arlene
 
Thank you both - it works now!

Arlene

swedbera said:
Could I add the same code to both the form and the subform? Will it also
cancel the navigation to another record when I add this to the BeforeUpdate
event, and if so, how to I move to another record once the user has answered
"no" when prompted to save record changes?

Arlene
 
swedbera said:
Could I add the same code to both the form and the subform? Will it also
cancel the navigation to another record when I add this to the BeforeUpdate
event, and if so, how to I move to another record once the user has answered
"no" when prompted to save record changes?

Yes but imagine a typical main form subform (1 to many) setup.

User enters main data and then tabs into first subrecord.

"Do you want to save those changes?"
Yes.

User enters first sub-record and tabs into second sub-record.

"Do you want to save those changes?"
Yes

User enters additional sub-records getting prompted EACH and EVERY time to
ask if he wants to save them. Before too long your users are going to be
looking for your head in a noose.

Prompting for saves is a thin-client (web browser) way of thinking and it
is done in those situations because it almost HAS to be done that way, not
because it's a good idea.
 
Back
Top