user292965
user292965

Reputation: 163

Run time error 3021- no current record for delete button

I have the following code for a delete button written in visual basics in Microsoft Access.

Private Sub Delete_Click()
If Not (Me.ComputerSubform.Form.Recordset.EOF And Me.ComputerSubform.Form.Recordset.BOF) Then
    If MsgBox("Are you sure to delete?", vbYesNo) = vbYes Then
        CurrentDb.Execute "DELETE FROM Computer " & _
                " WHERE PCSN=" & Me.ComputerSubform.Form.Recordset.Fields("PCSN")
      Me.ComputerSubform.Form.Requery    
    End If
   End If
End Sub

The first time it words fine. But when I try to delete another record I will encounter "Run time error 3021- no current record". I could not understand as the code look fines to me and there is data available. I would appreciate any help. Thanks!

P.S. I am sorry I can't post my table as it contain alot of confidential data.

Upvotes: 0

Views: 1311

Answers (2)

John W Fowler
John W Fowler

Reputation: 1

Try this:

dim rs as dao.recordset
set rs = currentdb.openrecordset("computers",  _
dbopendynaset)
rs.findfirst "[pcsn] =  " & _ 
me.computersubform.form!pcsn
'If pcsn is text instead of a number you need chr(34)
' on either side
if rs.nomatch = false  then 
  rs.edit
  rs.delete 
  rs.update
  rs.close
me.refresh
end if

Upvotes: 0

dbmitch
dbmitch

Reputation: 5386

Instead of

Me.ComputerSubform.Form.Requery

You should use
Me.ComputerSubform.Form.Recordset.Requery

This will update the underlying recordset with your latest delete

Upvotes: 1

Related Questions