[AccessD] Cancel the closing of a form

Steve Schapel miscellany at mvps.org
Wed Sep 5 13:40:18 CDT 2007


Rusty,

I would expect the form to still close if you click either 'Yes' or 'No' 
on the message box.  I thought that would be the desired behaviour.

In the case of 'Cancel', you don't want the form to close, right?  Ok, I 
now see that the idea I suggested before was over-simplistic.  Sorry. 
If you remove the native [X] Close button, and close the form via a 
command button on the form, I think this will work:

Private Sub YourCommandButton_Click()
    On Error GoTo abandon
    DoCmd.RunCommand acCmdSaveRecord
    DoCmd.Close acForm, Me.Name
abandon: If Err.Number = 2501 Then Exit Sub
End Sub

... and then the Before Update code as I suggested before:
Private Sub Form_BeforeUpdate(Cancel As Integer)
    Dim answer As Integer
    answer = MsgBox("Save changes?", vbYesNoCancel)
    If answer = vbNo Then
       Me.Undo
    ElseIf answer = vbCancel Then
       Cancel = True
    End If
End Sub

Regards
Steve


rusty.hammond at cpiqpc.com wrote:
> Steve,
> 
> When I put that code in the BeforeUpdate the form still closes.
> 



More information about the AccessD mailing list