Delete Record in Access Forms

How I can delete Records when I assign Recordsource to Table in Access
egovernmentAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Systems AnalystCommented:
You basically need these four pieces to implement deleting a record:

Create a Button on the form to Delete:

Private Sub btnDelete    
    On Error Resume Next    'To trap case where user cancels Delete
    DoCmd.RunCommand acCmdDeleteRecord
    If Err.Number = 0 Or Err.Number = 2501 Then
        'no action needed.  0 means no error occurred; 2501 means Delete was cancelled in the Delete event below.
    Else
        MsgBox "An unexpected error has occurred when attempting to Delete this record:" & vbCrLf & vbCrLf & _
                  Err.Number & "  " & Err.Description, vbOKOnly, "DELETE RECORD"
    End If
End Sub
-------------------------

Then,put this code in the following three Form events:

Private Sub Form_Delete(Cancel As Integer)
    Beep
   'This message or something similar
    If MsgBox("Are you SURE you want to DELETE this record?." & Chr(13) & _
          "This operation cannot be undone", _
           vbExclamation + vbYesNo + vbDefaultButton2, "Delete Record") <> vbYes Then
        Cancel = True
    End If
End Sub

Private Sub Form_BeforeDelConfirm(Cancel As Integer, Response As Integer)
  Response = acDataErrContinue
End Sub

Private Sub Form_AfterDelConfirm(Status As Integer)    
    Me.Requery
    DoEvents
End Sub
---------------
mx
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
Do you want to delete ALL the records in that Recordsource, or just individual records? Mx's code shows how to properly delete a record using the form's events, but if you want to do a bulk delete, you can do this:

Currentdb.Execute "DELETE * FROM YourTable"

Note this is not reversible, so be sure it's what you want to do. Once you run this, the records are gone and there is no Recycle Bin in Access!

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.