Connecting Tech Pros Worldwide Forums | Help | Site Map

Warning user of record deletion

Newbie
 
Join Date: Sep 2008
Posts: 7
#1: Sep 1 '08
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

Member
 
Join Date: Aug 2008
Posts: 77
#2: Sep 1 '08

re: Warning user of record deletion


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
missinglinq's Avatar
Moderator
 
Join Date: Nov 2006
Location: Richmond, Virginia USA
Posts: 3,000
#3: Sep 1 '08

re: Warning user of record deletion


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)>
Newbie
 
Join Date: Sep 2008
Posts: 7
#4: Sep 1 '08

re: Warning user of record deletion


Thanks both of you. Works just like I want it. Much Appreciated
ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,218
#5: Sep 2 '08

re: Warning user of record deletion


Quote:

Originally Posted by robertri

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
Newbie
 
Join Date: Sep 2008
Posts: 7
#6: Sep 2 '08

re: Warning user of record deletion


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

The code doesnt seem to work though?
ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,218
#7: Sep 2 '08

re: Warning user of record deletion


Quote:

Originally Posted by robertri

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?
Newbie
 
Join Date: Sep 2008
Posts: 7
#8: Sep 2 '08

re: Warning user of record deletion


Quote:

Originally Posted by ADezii

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
ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,218
#9: Sep 2 '08

re: Warning user of record deletion


Quote:

Originally Posted by robertri

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.
Reply