Delete Record in Form but then can't continue

S

smilee8_28

I have a delete button in a form. I can get it to delete. Originally via Undo
by calling the menu options per someone's recommendation but that always
deletes the last record not current or selected record. So I've tried
'DoCmd.Record.Delete' or something like that which works and currently using
just RecordSet.Delete both with a acCmdRecordGoToNew or Last or Previous.
These delete the data but gotonew etc are 'not available at this time' and
without that the current form after deletion doesn't work. I start entering
new info but it disappears when I leave the fields and I can't get out of the
form because warnings for required data pop up. I need a way to delete the
current form record whether it's the first record entered for the day (data
entry form) or the last where after deletion it goes either to the next
record or if the last record was deleted to a new record. Typically the user
will need to clear the current form and start with a fresh form.

Private Sub Clear_Form_Button_Click()
On Error GoTo Err_Clear_Form_Button_Click
Dim iAns As Integer
iAns = MsgBox("Are you sure you want to delete this record?", vbYesNo)
If iAns = vbNo Then
Cancel = True
Else
Recordset.Delete
DoCmd.RunCommand acCmdRecordsGoToNew
End If
Exit_Clear_Form_Button_Click:
Exit Sub

Err_Clear_Form_Button_Click:
MsgBox Err.Description
Resume Exit_Clear_Form_Button_Click

End Sub

I've searched through other questions but haven't found anything that works.

Thanks in advance for you help!
Kristine
 
S

smilee8_28

Actually, Undo is fine and may really be all I need/want but then I don't
want the 'Clear Form' button I have to show on previous forms because then
users will inadvertently delete the last record. Is that possible? I want a
button to be visible only on a new record being entered.

Thanks, Kristine
 
B

Brian Bastl

on forms current event

if not me.newrecord then
me.commandbutton.enabled=false
end if

Brian
 
D

Dirk Goldgar

smilee8_28 said:
Actually, Undo is fine and may really be all I need/want but then I
don't want the 'Clear Form' button I have to show on previous forms
because then users will inadvertently delete the last record. Is that
possible? I want a button to be visible only on a new record being
entered.

Thanks, Kristine

I'm not completely sure what you're after, Kristine, but I think this
may do what you want. If you're on a New record, it "undoes" it; if
you're on a record that has alreadyt been saved, it deletes it.

'----- start of code -----
Private Sub Clear_Form_Button_Click()

On Error GoTo Err_Clear_Form_Button_Click

Dim iAns As Integer

If Me.NewRecord Then

If Me.Dirty Then
If MsgBox( _
"Are you sure you want to discard " & _
"the data you've entered?", _
vbYesNo) _
= vbYes _
Then
Me.Undo
End If
End If

Else

' This is a record that has already been saved.
' It may still be dirty, though.

If MsgBox( _
"Are you sure you want to delete this record?", _
vbYesNo) _
= vbYes _
Then
If Me.Dirty Then Me.Undo
RunCommand acCmdDeleteRecord
RunCommand acCmdRecordsGoToNew
End If

End If

Exit_Clear_Form_Button_Click:
Exit Sub

Err_Clear_Form_Button_Click:
MsgBox Err.Description
Resume Exit_Clear_Form_Button_Click

End Sub
'----- end of code -----
 

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

Top