Delete record? Yes or No Message box

sgtblitz

Registered User.
Local time
Yesterday, 22:30
Joined
Feb 28, 2008
Messages
45
Hi guys, its a really quick question that im guessinbg would be easy to do, but is it possible for me to apply any code so that when a "delete record" button is pressed a message box would appear saying something like:

Are you sure you want to delete this record

Yes No


So when the Yes button is clicked it will perform the deletion and when the no button is clicked it will just return to that record.

Thanks a lot guys, ill upload my database for all too look at as ive almost finished
 
I use this on my database and it works exactly as you want...


Code:
Private Sub btnDeleteRecord_Click()
On Error GoTo Err_btnDeleteRecord_Click
Dim Answer As Integer

Answer = MsgBox("Are you sure you wish to delete this record?", vbYesNo + vbExclamation + vbDefaultButton2, "Delete Confirmation")
If Answer = vbYes Then
DoCmd.SetWarnings False
    DoCmd.DoMenuItem acFormBar, acEditMenu, 8, , acMenuVer70
    DoCmd.DoMenuItem acFormBar, acEditMenu, 6, , acMenuVer70
DoCmd.SetWarnings True

Else

End If

Exit_btnDeleteRecord_Click:
    Exit Sub

Err_btnDeleteRecord_Click:
    MsgBox Err.Description
    Resume Exit_btnDeleteRecord_Click
    
End Sub

Hope this helps

Regards - Ian
 
Thanks ian!

Works liek a charm :P

Take care

Daniel
 
I use this on my database and it works exactly as you want...


Code:
Private Sub btnDeleteRecord_Click()
On Error GoTo Err_btnDeleteRecord_Click
Dim Answer As Integer

Answer = MsgBox("Are you sure you wish to delete this record?", vbYesNo + vbExclamation + vbDefaultButton2, "Delete Confirmation")
If Answer = vbYes Then
DoCmd.SetWarnings False
    DoCmd.DoMenuItem acFormBar, acEditMenu, 8, , acMenuVer70
    DoCmd.DoMenuItem acFormBar, acEditMenu, 6, , acMenuVer70
DoCmd.SetWarnings True

Else

End If

Exit_btnDeleteRecord_Click:
    Exit Sub

Err_btnDeleteRecord_Click:
    MsgBox Err.Description
    Resume Exit_btnDeleteRecord_Click
    
End Sub

Hope this helps

Regards - Ian

Two things about the code...

1. Change these:
Code:
   DoCmd.DoMenuItem acFormBar, acEditMenu, 8, , acMenuVer70
     DoCmd.DoMenuItem acFormBar, acEditMenu, 6, , acMenuVer70

to these

Code:
    DoCmd.RunCommand acCmdSelectRecord
    DoCmd.RunCommand acCmdDeleteRecord

2. If you use this: DoCmd.SetWarnings False
Then make sure to put DoCmd.SetWarnings True it in the error handler as well because if you run the code and it fails before it gets to the DoCmd.SetWarnings True code then you will find yourself without warnings at all.

So
Code:
Err_btnDeleteRecord_Click:
[color=red]    DoCmd.SetWarnings True[/color]
    MsgBox Err.Description
    Resume Exit_btnDeleteRecord_Click
 
I have tried to use the above code and the message box works fine but the record is not deleted afterwards. Please excuse my ignorence I am new to the world of visual basic.

I am trying to delete all the records on the form including any that might be in the sub form as well ... could that be causing the problem ?
 
Last edited:
Also the:

DoCmd.DoMenuItem acFormBar, acEditMenu, 8, , acMenuVer70
DoCmd.DoMenuItem acFormBar, acEditMenu, 6, , acMenuVer70


may vary if the access version changed
 
DoMenuItem has been obsolete for years, use the RunCommand instead
 
I was using runcommand option and it wouldn't work.
I'm using access '07.
Message box working fine but the record is not deleting... seems so simple and it should work .. very frustrating

Thanks for the replies
 
if the code doesnt delete records, then maybe you have a non-updateable query for some reason.
 
You could try running the delete through SQL. Something like:

Code:
DoCmd.RunSQL ("DELETE TableName.* " & _
          "FROM TableName " & _
          "WHERE TableName.IDFieldName = " & Me![IDFieldName] & "; ")
 
Hi there... This is my first post in the forum :)

This code works fine, but it doesn t delete the record from the table.

I' ve a mainform and a subform. When i fill the data in the main form and then setfocus in the subform this automatically write the data in the table.

But how can i cancel this, for example an order, in the middle of process?

Delete the last record from table, undo the operation, check my relationships between tables?

Thankz and congratulations to all users and admins by posting everyday many hellpfull information like this one.
 
Last edited:
It helps so Good.. thank you:D

I use this on my database and it works exactly as you want...


Code:
Private Sub btnDeleteRecord_Click()
On Error GoTo Err_btnDeleteRecord_Click
Dim Answer As Integer

Answer = MsgBox("Are you sure you wish to delete this record?", vbYesNo + vbExclamation + vbDefaultButton2, "Delete Confirmation")
If Answer = vbYes Then
DoCmd.SetWarnings False
    DoCmd.DoMenuItem acFormBar, acEditMenu, 8, , acMenuVer70
    DoCmd.DoMenuItem acFormBar, acEditMenu, 6, , acMenuVer70
DoCmd.SetWarnings True

Else

End If

Exit_btnDeleteRecord_Click:
    Exit Sub

Err_btnDeleteRecord_Click:
    MsgBox Err.Description
    Resume Exit_btnDeleteRecord_Click
    
End Sub
Hope this helps

Regards - Ian
 

Users who are viewing this thread

Back
Top Bottom