Link to home
Start Free TrialLog in
Avatar of LiCann
LiCann

asked on

Delete Button

Will this be OK for delete button or do I need to change?
Private Sub DeleteButton_Click()


On Error GoTo Err_DeleteButton_Click

  If MsgBox("Delete this record?", vbOKCancel) = vbCancel Then
    Cancel = True
   
    Else
    DoCmd.DoMenuItem acFormBar, acEditMenu, 8, , acMenuVer70
    DoCmd.DoMenuItem acFormBar, acEditMenu, 6, , acMenuVer70

Exit_DeleteButton_Click:
    Exit Sub

Err_DeleteButton_Click:
    MsgBox Err.Description
    Resume Exit_DeleteButton_Click
   
Avatar of Scott McDaniel (EE MVE )
Scott McDaniel (EE MVE )
Flag of United States of America image

There is no Cancel argument for a command button click ... if you're trying to confirm a delete, do this:

If MsgBox("Delete this record?", vbOKCancel) <> vbOK Then
  Exit Sub
Else
  Currentdb.Execute "DELETE * FROM YourTAble WHERE YourID=" & Me.YourIDField
  Me.Requery '/to refresh the recordset
End If
 
I'd try to avoid the DoMenuItem where possible ...
Avatar of thenelson
thenelson

Cancel = True
only works for those events that passes a cancel argument like before update:
Private Sub MyTextBox_BeforeUpdate(Cancel As Integer)

So
Cancel = True
will not work for the click event.

BTW:
    DoCmd.DoMenuItem acFormBar, acEditMenu, 8, , acMenuVer70
    DoCmd.DoMenuItem acFormBar, acEditMenu, 6, , acMenuVer70
are archaic.   Instead of those use:
RunCommand acCmdSelectRecord
RunCommand acCmdDeleteRecord

Reference:  http://home.clara.net/tkwickenden/

So instead the code would be:

Private Sub DeleteButton_Click()

On Error GoTo Err_DeleteButton_Click

  If MsgBox("Delete this record?", vbOKCancel) = vbOk Then
      RunCommand acCmdDeleteRecord
  End If

  Exit Sub

Err_DeleteButton_Click:
    MsgBox Err.Description

End Sub

ASKER CERTIFIED SOLUTION
Avatar of DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial