DoCmd.RunCommand acCmdDeleteRecord

livvie

Registered User.
Local time
Today, 11:05
Joined
May 7, 2004
Messages
158
I am using the following command to delete records.
DoCmd.RunCommand acCmdDeleteRecord which is fine if the user presses OK but if the user presses No it just crashes. How do I code this with my own error messages. Below is what I have so far but that deletes whether you press or no as the delete is already done.

Dim Msg, Style, Title, Response
DoCmd.SetWarnings False
DoCmd.RunCommand acCmdDeleteRecord
Msg = "You are about to delete a line. Do you really want to do this?" ' Define message.
Style = vbYesNo + vbCritical + vbDefaultButton2 ' Define buttons.
Title = "Warning" ' Define title.
' Display message.
Response = MsgBox(Msg, Style, Title)
If Response = vbYes Then ' User chose Yes.
Else ' User chose No.
DoCmd.Hourglass False
Exit Sub
End If
 
Code:
Private Sub cmdButton_Click()
  On Error GoTo ProcErr

  If MsgBox(You are about to delete a line. Do you really want to do this?", vbYesNo + vbCritical + vbDefaultButton2, "Warning") = vbYes Then
    DoCmd.SetWarnings False
    DoCmd.RunCommand acCmdDeleteRecord
    DoCmd.SetWarnings True
  End If

ProcExit:
  Exit Sub

ProcErr:
  If Err=2501 Then ' user cancelled
    Resume Next
  Else
    MsgBox Err.Number & ": " & Err.Description
    Resume ProcExit
End Sub
 
SforSoftware said:
Code:
Private Sub cmdButton_Click()
  On Error GoTo ProcErr

  If MsgBox(You are about to delete a line. Do you really want to do this?", vbYesNo + vbCritical + vbDefaultButton2, "Warning") = vbYes Then
    DoCmd.SetWarnings False
    DoCmd.RunCommand acCmdDeleteRecord
    DoCmd.SetWarnings True
  End If

ProcExit:
  Exit Sub

ProcErr:
  If Err=2501 Then ' user cancelled
    Resume Next
  Else
    MsgBox Err.Number & ": " & Err.Description
    Resume ProcExit
End Sub


Thanks worked perfectly.
 

Users who are viewing this thread

Back
Top Bottom