errors while deleting a record

Nirious

Registered User.
Local time
Today, 09:29
Joined
Jul 31, 2004
Messages
106
I have the following code which I use to delete records.The code works just fine on my old laptop. I recently got myself a new laptop and reinstalled the whole bunch. Now I'm having the all kinds of errors thrown at me that I didn't have before.

3021 : No current record
2046 : The command 'DeleteRecord' isn't available now
2046 : The command '|' isn't available now

I'm using MS acces 2002 SP3

Here is the code:
Code:
Private Sub cmdDelete_this_record_Click()
On Error GoTo Err_cmdDelete_this_record_Click
If Me.Input = "w" Then
    MsgBox LS(2010, , 1), vbInformation + vbOKOnly, LS(2010, , 2)
    Exit Sub
End If

If MsgBox(LS(2011, , 1), vbInformation + vbDefaultButton2 + vbYesNo, LS(2011, , 2)) = vbYes Then
    If Me.Recordset.RecordCount = 1 Then
        MsgBox LS(2012, , 1), vbInformation, LS(2012, , 2)
        Exit Sub
    End If
    DoCmd.SetWarnings False
    DoCmd.RunCommand acCmdSelectRecord
    DoCmd.RunCommand acCmdDeleteRecord
    DoCmd.SetWarnings True
End If
Exit_cmdDelete_this_record_Click:
    Exit Sub

Err_cmdDelete_this_record_Click:
    If Err.Number = 2465 Then
        'This error is ignored, it doen't affect the delete process
    Else
       MsgBox Err.Description & Err.Number
    End If
    Resume Exit_cmdDelete_this_record_Click
    
End Sub

Don't mind the LS(number), it's a multilingual application. It basically just asks if you are sure you want to delete the record.

Now the weird thing is:
Sometimes I don't get the error and it removes the record just fine, sometimes I get error 3021, sometimes error 2046. And if I click the delete button of the record it might give me errors but if I keep clicking it, it will stop giving the error eventually and delete it without a problem!!! :confused:

So e.g. I click on "delete"
=>error 2046 (record remains)
I click it again
=> error 2046 (record remains)
I click it again
No poblem, it just delete the stupid record.
So if it can delete the record why doesn't it do it right from the first time :mad:


PLz help me. I have no clue what I'm doing wrong here. How can it work some of the time and be such a pain in the ass the other. I really hate those errors that I have no control over.

I could just ignore the errors but than the user would sometimes have to three times press the delete-button to delete his record. That can't be right.
 
Last edited:
You know what. I couldn't get it to work like I wanted it too with this code.
So I just deleted the records directly within the DB. That does work!
I will never again use acCmdDeleteRecord. It's not worth the throuble.

Now I use this code. Works like a charm.
Code:
    Dim cmd As New ADODB.Command
    Dim deletestring As String
    cmd.CommandType = adCmdText
    cmd.ActiveConnection = CurrentProject.Connection
    deletestring = "DELETE ArtikelsOpBon.* FROM ArtikelsOpBon WHERE (((ArtikelsOpBon.id)= " & id & "));"
    cmd.CommandText = deletestring
    cmd.Execute
    Me.Requery
 

Users who are viewing this thread

Back
Top Bottom