By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,041 Members | 1,858 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,041 IT Pros & Developers. It's quick & easy.

Warning user of record deletion

P: 7
Hi,

I need help creating a dialog box. Basically I have a delete record button in my DB, but when clicked, I want a dialog box popping up saying something like "Are you sure you want to delete this record?"

So Im guessing Im going to need some sort of "IF" statement when the button is clicked...but im lost since I dont know much about programming. If anybody could help I would appreciate it.

Thanks,

Rob
Sep 1 '08 #1
Share this Question
Share on Google+
8 Replies


P: 77
You may use something like this:

Expand|Select|Wrap|Line Numbers
  1. Dim vChoice as String
  2. vChoice = MsgBox("Are you sure you want to delete?", vbQuestion, "Confirm")
  3. If vChoice = vbYes Then
  4.    'Proceed further
  5. Else
  6.     Exit Sub
  7. End If
Sep 1 '08 #2

missinglinq
Expert 2.5K+
P: 3,532
Line #2 of that code needs to be changed from

vChoice = MsgBox("Are you sure you want to delete?", vbQuestion, "Confirm")

to

vChoice = MsgBox("Are you sure you want to delete?", vbQuestion + vbYesNo, "Confirm")

When vbQuestion is used by itself, a single "Okay" button appears, so the user has no real choice. And since clicking "Okay" doesn't return vbYes, the code will branch to the Else statement and the deletion will never happen.

The Dim statement, if used, should really be

Dim vChoice as Integer

as that's what message boxes actually return. vbYes is a Constant for 6 and vbNo is a Constant for 7. Fortunately, Access is very forgiving in this, and reads the String 6 or 7 as a number.

Welcome to Bytes!

Linq ;0)>
Sep 1 '08 #3

P: 7
Thanks both of you. Works just like I want it. Much Appreciated
Sep 1 '08 #4

ADezii
Expert 5K+
P: 8,638
Thanks both of you. Works just like I want it. Much Appreciated
There is only 1 major problem with the previous approach. What if a User Deletes a Record via the Menu Bar or Toolbar? The following code, placed in the BeforeDelConfirm() Event, will cover all contingencies:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_BeforeDelConfirm(Cancel As Integer, Response As Integer)
  2. Dim intResponse As Integer
  3.  
  4. 'Suppress the Access Delete Confirmation Dialog Box, do not
  5. 'prompt the User
  6. Response = acDataErrContinue
  7.  
  8. 'Display your own Custom Delete Dialog Box
  9. intResponse = MsgBox("Delete Record(s)?", vbQuestion + vbYesNo + vbDefaultButton1, _
  10.                      "Record(s) Deletion")
  11.  
  12. If intResponse = vbYes Then
  13.   'do nothing and just fall through
  14. Else
  15.   Cancel = True     'Cancel the Deletion (don't Delete Record(s))
  16. End If
  17. End Sub
Sep 1 '08 #5

P: 7
It would be very good to have a warning through using the toolbar or menu.

The code doesnt seem to work though?
Sep 2 '08 #6

ADezii
Expert 5K+
P: 8,638
It would be very good to have a warning through using the toolbar or menu.

The code doesnt seem to work though?
What exactly is the error you are getting?
Sep 2 '08 #7

P: 7
What exactly is the error you are getting?

No Error, it just deletes the record without asking a dialog box.

Its like its not executing Form_BeforeDelConfirm. The code is there but not being used
Sep 2 '08 #8

ADezii
Expert 5K+
P: 8,638
No Error, it just deletes the record without asking a dialog box.

Its like its not executing Form_BeforeDelConfirm. The code is there but not being used
The code is sound and fully functional. There are only 3 reasons that I can think of as to why it isn't working for you:
  1. The code is not in the BeforeDelConfirm() Event Procedure of the Form.
  2. The code is in the correct Procedure, but it is not the exact code that I had previously posted.
  3. The words [Event Procedure] do not exist in the Before Del Confirm Property of the Form.
Sep 2 '08 #9

Post your reply

Sign in to post your reply or Sign up for a free account.