Save changes prompt

  • Thread starter Thread starter NH
  • Start date Start date
N

NH

I have a form which has two buttons at the bottom:
OK (check fields have been filled in correctly and close
form)
and a Cancel (undo current record, and close form)

These work perfectly, but it doesn't stop users from
hitting the X on the title bar and bypassing the checks..

I have moved the checks to the Form_Unload event, and set
the button to just close the form (because the checks will
be made as the form closes), but now I get a
message 'Close action was cancelled' when the button is
pressed..

I have attached a snippet of the code:

Prvate Sub BtnCloseForm_btn_Click()
DoCmd.Close , , acSaveYes
End Sub

Private Sub Form_Unload(Cancel As Integer)
If Me.NewRecord = True And Me.Dirty = False Then Exit Sub
'<Run Checks>
If strChecksPassed = True Then Exit Sub
'<Inform user of mistakes>
Cancel = True
End Sub

I think I understand why it is cancelling the close
command, but I can't think of any way to get around it..

Can someone suggest a way?

Thank you

NH
 
You could edit the properties of the form and set
the "control box" to NO. That takes away the X in the
upper right hand corner.

HTH

Bobby
 
Form_Unload is too late. The changes have already been saved by then. Use
Form_BeforeUpdate - the event that fires just before the changes are written
to disk.

Example:

Private Sub Form_BeforeUpdate(Cancel As Integer)
If IsNull(Me.City) Then
Cancel = True
MsgBox "You forgot to fill in the City."
End If
End Sub
 
Hi Allen,
I have similar problem and tried your method.
The problem is that form will close even if you have this code set. In
other words there is no opportunity for the user to change their entry.
Setfocus and me.undo also didn't work. Unless there is a way to 'undo' the
close command.
 
Back
Top