Close Button

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

Guest

I have a form with the below code in the BeforeUpdate event.

I created a 'New Record' button: DoCmd.GoToRecord , , acNewRec
and a 'Close' button: DoCmd.Close, but they aren't working right. When
they are clicked the message box "Do you want to save this new log?" appears
(which it should). However, even if a user clicks "yes" to save a log, the
DoCmd.GoToRecord or DoCmd.Close are carried out. How can I stop the going to
a new record or closing if a user wants to save the log? Thanks.

Private Sub Form_BeforeUpdate(Cancel As Integer)
If Me.NewRecord Then
If MsgBox("Do you want to save this new log?", vbYesNo) = vbNo Then
Cancel = True
Me.Undo
Exit Sub

End If
End If


If IsNull(Me.Part__) Then
MsgBox "There is information missing.", vbOKOnly
Cancel = True
End If
End Sub
 
Move this part of the code to your other events.
If Me.NewRecord Then
If MsgBox("Do you want to save this new log?", vbYesNo) = vbNo Then
Cancel = True
Me.Undo
Exit Sub

If Me.NewRecord Then
If MsgBox("Do you want to save this new log?", vbYesNo) = vbNo Then
Me.Undo
DoCmd.GoToRecord , , acNewRec
End If
End If

If Me.NewRecord Then
If MsgBox("Do you want to save this new log?", vbYesNo) = vbNo Then
Me.Undo
DoCmd.Close
End If
End If

The issue is that the Cancel only affects updating the record in the Before
Update, it does not cancel any other commands issused in any event. So you
need to ask the question before you take the action.
 
Perfect! Thank you.

Klatuu said:
Move this part of the code to your other events.

If Me.NewRecord Then
If MsgBox("Do you want to save this new log?", vbYesNo) = vbNo Then
Me.Undo
DoCmd.GoToRecord , , acNewRec
End If
End If

If Me.NewRecord Then
If MsgBox("Do you want to save this new log?", vbYesNo) = vbNo Then
Me.Undo
DoCmd.Close
End If
End If

The issue is that the Cancel only affects updating the record in the Before
Update, it does not cancel any other commands issused in any event. So you
need to ask the question before you take the action.
 
Back
Top