Runtime Error on Delete Code

M

Michelle

I have the following code connected to a command button that is used to
delete a record ... It gives me an error Run-time error '3021': No current
record.
This is on a form that is pulled from query where you enter the key # it
bring the key up you can then either edit the information for that key or
delete it.

Also it errors out at " DoCmd.RunCommand acCmdDeleteRecord"

Private Sub Command4_Click()
Dim MsgStr As String
Dim TitleStr As String
MsgStr = "Are You Sure You Want To Delete This Record?"
TitleStr = "Confirm Delete Record"
If MsgBox(MsgStr, vbYesNo, TitleStr) = vbNo Then
Exit Sub
End If
DoCmd.SetWarnings False
DoCmd.RunCommand acCmdDeleteRecord
DoCmd.SetWarnings True

End Sub
 
D

Dale Fye

Michelle,

I assume that this command button is one of your own making.

Try adding some code to the Forms current event that disables the button if
no record is selected or if there are no records in the current recordset.
Something like:

Private Sub Form_Current

me.Command4.Enabled = (me.recordsetclone.recordcount > 0)

Exit sub

You might also want to define an long integer variable (lngCurRec) in your
click event, and then try to set its value prior to the other code and trap
the NoCurrentRecord error, something like:

Private Sub cmd_DelRecord_Click()

Dim MsgStr As String, TitleStr As String
Dim lngCurRec as long

On Error Resume Next
lngCurRec = me.currentrecord
if err.number <> 0 then
msgbox "No current record"
exit sub
end if

On Error Goto Err_Handler
MsgStr = "Are You Sure You Want To Delete This Record?"
TitleStr = "Confirm Delete Record"
If MsgBox(MsgStr, vbYesNo, TitleStr) = vbNo Then Exit Sub

DoCmd.SetWarnings False
DoCmd.RunCommand acCmdDeleteRecord
DoCmd.SetWarnings True
Exit Sub

Err_Handler:
msgbox err.number & vbcrlf & err.description

End Sub
 

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