Link to home
Start Free TrialLog in
Avatar of marsbar105
marsbar105

asked on

Trying to add a msgbox with 'yes or no' buttons before delete record?

Hi,

I'm trying to add a msgbox with 'yes or no' buttons before the delete process removes a record?

I have been playing around with macros a bit but don't have much to go on.

Any help would be appreciated,

Mars
(a bit of a newbie)
Avatar of helixcon
helixcon

Try this

Private Sub Form_BeforeDelConfirm(Cancel As Integer, Response As Integer)
       If MsgBox("Confirm deletion", 1) = vbOK Then
            Response = False
            Cancel = False
        Else
            Cancel = True
            Response = False
       
         End If
End Sub


Please note that on the Tools,Options,EditFind,Confirm,Record Changes must be turned on as the BeforeDelConfirm is triggered before the default message is displayed. Paste this on the form (or subform) you are working on
For the Message Box to use Yes No buttons the you can change the example above to read

If MsgBox("Confirm deletion", vbYesNo) = vbYes Then

Cheers, Andrew
Here is the VB Code for Yes, No, Cancel Mesage Box...Modify it to suit your needs.If you don't want Canel Option Change "vbYesNoCancel" to "vbYesNo"...ACSPanama


'(MssBox Yes, No, Cancel)
    If IsNull([Combo14]) = False Or Me![SubTotal] = 0 Then
        Dim Msg, Style, Title, Help, Ctxt, Response, MyString
        Msg = "Update This Customers Information." & Chr$(10) & Chr$(13) & "Add a New Customer to the Customer Database."  
        Style = vbYesNoCancel + vbInformation + vbDefaultButton1   ' Define buttons.
        Title = "Update This Customer  or  Add a New Customer"    ' Define title.
        Response = MsgBox(Msg, Style, Title, Help, Ctxt)
        If Response = vbYes Then GoTo Skip1   ' User choose Yes.
        MyString = "Yes"    ' Perform some action.
        If Response = vbNo Then GoTo Skip0
        MyString = "No"    ' Perform some action.
        If Response = vbCancel Then GoTo ByeBye
        MyString = "Cancel"
        End If
Avatar of marsbar105

ASKER

Everyone,

Where do I put this code, and am I still using the macro for msgbox??

Here is some detail: This whole thing is for a "delete asset" button on a form.

Thanks,
Mars
Here is another alternative:

Dim confirm As Integer
confirm = MsgBox("Are you sure you want to delete this record?", vbQuestion + vbYesNo, "Confirm Delete")
If confirm = 6 Then 'User clicked Yes
  DoCmd.SetWarnings False 'prevent a second confirm message
  DoCmd.RunCommand acCmdDeleteRecord
  DoCmd.SetWarnings True
End If

Whether you use this code or someone else's suggestion, it should be tied to the OnClick event of the button.

In design view, right click the button and select "Properties". Under the "Events" tab, find "OnClick" and select "[Event Procedure]" from the drop-down list (Combo Box) to the right. Then, click the "..." button to the right to open the VB editor and paste the code in there.
paste the following code on the "on click" event of the button you are using to erase the record:

       Me.AllowDeletions = True
            DoCmd.DoMenuItem acFormBar, acEditMenu, 8, , acMenuVer70
            DoCmd.DoMenuItem acFormBar, acEditMenu, 6, , acMenuVer70
        Me.AllowDeletions = False

This will allow you to delete the record in case the record set is blocked to avoid manual deletions. Then just copy and paste the code listed on my previous comment right below (or before) the erase button sub (after the End Sub of the On Click event for the button in question).
The msgbox comand is inserted wherever you need the macro to stop and give you the confirm question

Private Sub Form_BeforeDelConfirm(Cancel As Integer, Response As Integer)
 etc....
End Sub
this is triggered when the default access delete confirmation is given.

ASKER CERTIFIED SOLUTION
Avatar of ACSPanama
ACSPanama

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
Thank you very much everyone.  All very good answers.

Mars