Delete record? Yes or No Message box (1 Viewer)

sgtblitz

Registered User.
Local time
Today, 01:16
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
 

ianward

Registered User.
Local time
Today, 08:16
Joined
May 1, 2007
Messages
52
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
 

sgtblitz

Registered User.
Local time
Today, 01:16
Joined
Feb 28, 2008
Messages
45
Thanks ian!

Works liek a charm :p

Take care

Daniel
 

boblarson

Smeghead
Local time
Today, 01:16
Joined
Jan 12, 2001
Messages
32,059
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
 

databaser

New member
Local time
Today, 08:16
Joined
Mar 21, 2010
Messages
2
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:

Khalid_Afridi

Registered User.
Local time
Today, 11:16
Joined
Jan 25, 2009
Messages
491
Also the:

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


may vary if the access version changed
 

Rich

Registered User.
Local time
Today, 08:16
Joined
Aug 26, 2008
Messages
2,898
DoMenuItem has been obsolete for years, use the RunCommand instead
 

databaser

New member
Local time
Today, 08:16
Joined
Mar 21, 2010
Messages
2
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
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 08:16
Joined
Sep 12, 2006
Messages
15,614
if the code doesnt delete records, then maybe you have a non-updateable query for some reason.
 

Pyro

Too busy to comment
Local time
Today, 19:16
Joined
Apr 2, 2009
Messages
126
You could try running the delete through SQL. Something like:

Code:
DoCmd.RunSQL ("DELETE TableName.* " & _
          "FROM TableName " & _
          "WHERE TableName.IDFieldName = " & Me![IDFieldName] & "; ")
 

kameyke

New member
Local time
Today, 01:16
Joined
Feb 10, 2010
Messages
2
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:

azizmech

New member
Local time
Today, 11:16
Joined
Jan 31, 2016
Messages
3
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

Top Bottom