injanib via AccessMonster.com said:
The form on which I am using the code has a subform too. The code gives me
the proper prompt if I make a change on the main form, but not on the
subform.
Meaning that if I make a change in the subform and then press the
navigating
buttons, it does not prompt whether I want to save the changes or not. It
automatically saves them and moves on.
That happens because whenever you change the focus from the subform to the
main form, the subform record is automatically saved if it has been
modified. The same thing happens (in reverse) if you change from the main
form to a subform: the main form's record is automatically saved if it is
"dirty". There is nothing you can do to stop this behavior of Access, short
of using unbound forms or subforms and handling the loading and unloading of
data yourself -- thereby abandoning the power of MS Access.
Your original question was about a navigation button, but I think you really
have a larger issue in mind -- you want to control whether or not a record
gets saved, no matter how that might happen. For example, a dirty record on
the main form would normally be saved automatically whenever the user moves
to a different record, or when the focus is shifted to the subform, or when
the form is closed. If you want to trap the save and require the user to
confirm or discard the changes under all those circumstances, then you
really need to be working in the form's BeforeUpdate event. For example,
you might have a BeforeUpdate event procedure like this:
'------ start of code ------
Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim strMessage As String
strMessage = "Do you want to save " & _
IIf(Me.NewRecord, "", "your changes to ") & "this record?"
If MsgBox(strMessage, vbQuestion+vbYesNo, "Save or Discard?") _
= vbNo _
Then
' Discard changes to this record.
Me.Undo
End If
End Sub
'------ end of code ------
If you want to force the user to confirm changes for both the main form and
a subform, you would have to have a similar event procedure for the
BeforeUpdate event of both forms. Note, though, that you will not easily be
able to arrange things so that the user enters a main record and then one or
more subform records, and only then gets a prompt to confirm parent and
child records alike. That's because the user can't get to the subform
without automatically saving any changes that have been made to the main
form's record. It is possible, through some complex mechanisms, to make
this appear to happen, but I recommend you not try to go there.