Delete code competing with BeforeUpdate validation

K

Kurt Heisler

I put code in the Before Update event of my form to verify that two
fields are complete (i.e., Not Null) before saving. The code is:

Private Sub Form_BeforeUpdate(Cancel As Integer)

If IsNull(Me.FirstName) Then
MsgBox "Please enter a first name."
Cancel = True
Me.FirstName.SetFocus
ElseIf IsNull(Me.LastName) Then
MsgBox "Please enter a last name."
Cancel = True
Me.LastName.SetFocus
End If

End Sub

Also on my form is a Delete button that deletes the record. The
problem is that if the user starts a new record, and then clicks the
delete button without having completed one of the two fields, the
Before Update event is triggered and he's asked to, "Please enter
a ... name." Any way to override this?
 
A

Allen Browne

Undo the record before deleting it.

If Me.Dirty Then Me.Undo
If Not Me.NewRecord Then RunCommand acCmdDeleteRecord
 
J

Jeanette Cunningham

Change your delete code to check for a dirty record and a new record before
deleting the record.

If Me.Dirty = True Then
Me.Undo
End If

If Not Me.NewRecord Then
'the code to delete goes here
End If


Jeanette Cunningham MS Access MVP -- Melbourne Victoria Australia
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

BeforeUpdate Problem 7
Data validation 16
exit form button and BeforeUpdate form event 4
BeforeUpdate, what's going wrong? 9
clear and delete 14
BeforeUpdate event 3
BeforeUpdate problem 4
BeforeUpdate Cancel 0

Top