I have a form that has a close button. If the record has been modified and the record has not saved (the record is 'dirty'), then a message box appears asking the user if the record should be saved.
My problem is that even if the user selects no and the cancel event happens, the routine still goes to the BeforeUpdate method and the record is still saved with the changes, even though the user has selected 'no'. The code is below:
Private Sub cmdClose_Click()
Dim retval As Variant
If Me.Dirty Then
retval = MsgBox("Do you want to save record?", vbYesNo, "Important")
If retval = vbYes Then
DoCmd.RunCommand acCmdSaveRecord
Else
DoCmd.CancelEvent
End If
End If
DoCmd.Close acForm, "frmResponses", acSaveNo
End Sub
Private Sub Form_BeforeUpdate(Cancel As Integer)
If IsNull(Me!company) Then
MsgBox "You must enter a company name!"
Cancel = True
DoCmd.CancelEvent
End If
End Sub
Any assistance would be greatly appreciated!
Rob
My problem is that even if the user selects no and the cancel event happens, the routine still goes to the BeforeUpdate method and the record is still saved with the changes, even though the user has selected 'no'. The code is below:
Private Sub cmdClose_Click()
Dim retval As Variant
If Me.Dirty Then
retval = MsgBox("Do you want to save record?", vbYesNo, "Important")
If retval = vbYes Then
DoCmd.RunCommand acCmdSaveRecord
Else
DoCmd.CancelEvent
End If
End If
DoCmd.Close acForm, "frmResponses", acSaveNo
End Sub
Private Sub Form_BeforeUpdate(Cancel As Integer)
If IsNull(Me!company) Then
MsgBox "You must enter a company name!"
Cancel = True
DoCmd.CancelEvent
End If
End Sub
Any assistance would be greatly appreciated!
Rob