473,395 Members | 1,418 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Deleting Records in Form View: stay on record selected prior to deletion

With all Access databases I have used, when I select to delete a record in Form view, the next record becomes the background for the message asking if I really want to delete a record. That makes it difficult to confirm that I really did select the record I wanted to get rid of. Can I make it stay on the record selected, prior to deletion?
Jan 13 '13 #1

✓ answered by Seth Schrock

What I would do would be to create a button that deletes the record. In the On_Click event of the button, it would first display a messagebox asking if you want to delete the record with VbYesNo buttons. If yes, then set the warnings to false and then delete the current record and then set the warnings back to true (so that you don't accidentally leave it with the warnings off). This way, the form doesn't know that you are deleting the record until after you have confirmed that you do want to delete the record.

10 2061
zmbd
5,501 Expert Mod 4TB
A few ways to do this depending on the form.
What I use is the form's before delete events, wherein I use a message-box that shows something about the selected record and asks for confirmation. This works well for single record deletions; however, for multiple records there are a few other ways to trap and confirm.
Jan 13 '13 #2
zmbd
5,501 Expert Mod 4TB
As you didn't mention which version of Access you are using, I defaulted back to v2003 when looking at my notes.
You'll find the following to be helpful I hope:
Delete Event - Access 2003 VBA Language Reference
Jan 13 '13 #3
Seth Schrock
2,965 Expert 2GB
What I would do would be to create a button that deletes the record. In the On_Click event of the button, it would first display a messagebox asking if you want to delete the record with VbYesNo buttons. If yes, then set the warnings to false and then delete the current record and then set the warnings back to true (so that you don't accidentally leave it with the warnings off). This way, the form doesn't know that you are deleting the record until after you have confirmed that you do want to delete the record.
Jan 13 '13 #4
ADezii
8,834 Expert 8TB
@Seth Schrock
  1. You must Trap any Error that can occur while attempting to DELETE a Record in order that you can Reset SetWarnings.
  2. You should make sure that the User cannot DELETE a Record by any other means except by your Command Button.
  3. That being said:
    Expand|Select|Wrap|Line Numbers
    1. Private Sub Command17_Click()
    2. On Error GoTo Err_DeleteRecord
    3. Dim strMsg As String
    4. Dim intResponse As Integer
    5. Dim intParams As Integer
    6.  
    7. intParams = vbCritical + vbYesNo + vbDefaultButton1
    8. strMsg = "DELETE this Record?" & vbCrLf & vbCrLf & _
    9.          "This process will be irreversible!"
    10.  
    11. intResponse = MsgBox(strMsg, intParams, "Record Deletion Confirmation")
    12.  
    13. If intResponse = vbYes Then
    14.   With DoCmd
    15.     .SetWarnings False
    16.       .RunCommand acCmdDeleteRecord
    17.     .SetWarnings True
    18.   End With
    19. End If
    20.  
    21. Exit_DeleteRecord:
    22.   Exit Sub
    23.  
    24. Err_DeleteRecord:
    25.   MsgBox Err.Description, vbExclamation, "Error Deleting Record"
    26.     DoCmd.SetWarnings True      'Must RESET
    27.       Resume Exit_DeleteRecord
    28. End Sub
@Seth:
My apologies, Seth. Somehow I thought that you were actually asking the question and not the OP. Well, I'll leave the Code intact anyway. Sorry.
Jan 13 '13 #5
zmbd
5,501 Expert Mod 4TB
Just as a point of information: The OnDelete and BeforeDelConfirm events trip before any default messages are fired.

By personal preference, I also tend to leave the default program message... that way the user not only has to go thru my prompt but a second prompt as well... makes denying the records, and deleting the same, by accident less likely and the user more liable for the event!
Jan 13 '13 #6
Seth Schrock
2,965 Expert 2GB
@ADezii Good point about error trapping. I have just started getting into the habit of using error trapping, but it isn't yet engraved in my mind. Otherwise, that was exactly what I had in mind.

@Z More of a personal preference, but I like removing all the default error messages from Access. However, that is definitely something to consider of putting more responsibility on the user for an accidental deletion. One database that I did I really wanted to make sure that records from a certain table weren't deleted by accident so I had a checkbox that enabled the deleted button. Selecting the checkbox prompted a message asking if you were sure you wanted to enable it. If you said yes, then the delete button was enabled. Then clicking delete prompted another message asking if you wanted to delete the record. Possibly a bit overkill, but deleting records is rare and I was having fun that day thinking about all the things that I could do :)
Jan 13 '13 #7
NeoPa
32,556 Expert Mod 16PB
That'll teach me to leave a question and come back to it later :-( Everyone's answered it already.

I would suggest that you design your forms in such a way that deleting records cannot occur unless via the interface you provide. That means adding a Delete button to the form (or any other interface trigger you choose) and disabling the Access provided Remove Record.

Others here have already covered how to disable other, automatic, messages (including DoCmd.SetWarnings), so only what you want to see should ever be shown. Personally, I would not recommend showing a message for the same thing twice, as users get more used to ignoring messages that way, and then things get done that shouldn't be. However, I'd happily recommend Seth's idea of requiring the operator to enable the delete triggering button if you want extra care to be shown. Ingeniously, it does that same job without the user realising it.
Jan 13 '13 #8
Time differences meant I didn't get to see the later messages until I'd incorporated Seth's initial solution which was simple to implement and is just the job, although the ensuing advice about the general principles on warnings, over-use etc is taken on board. There is no other means of deleting a record on this database other than this button so I think it's watertight.
Thanks to all for the good advice.
Jan 14 '13 #9
TheSmileyCoder
2,322 Expert Mod 2GB
Have you tried in your application, while the form is active to hit "Ctrl-A" and then hit Del? Or "Shift-Spacebar" and then hit delete?


Now my usual approach is this:
Make sure that AllowDeletions=False for the form. Then use code behind a delete button like so:
Expand|Select|Wrap|Line Numbers
  1. Private Sub btn_Delete_Click()
  2. On Error GoTo ErrHandler:
  3.    If vbYes = MsgBox("Do you wish to delete active record?", vbYesNo + vbExclamation) Then
  4.       Me.AllowDeletions = True
  5.       DoCmd.SetWarnings False
  6.       DoCmd.RunCommand acCmdDeleteRecord
  7.    End If
  8.  
  9. ExitSub:
  10.    DoCmd.SetWarnings True
  11.    Me.AllowDeletions = False
  12.    Exit Sub
  13.  
  14. ErrHandler:
  15.    MsgBox Err.Number & " - " & Err.Description
  16.    Resume ExitSub
  17. End Sub
This basically turns on deletions for when its needed and then removes the ability straight after.
Jan 14 '13 #10
NeoPa
32,556 Expert Mod 16PB
@Nick. Good to see such a gracious and intelligent response. I have no doubt you've progressed from the answers submitted here and I'm pleased for you :-)

@Smiley. There are other approaches to this which I was going to go into the details of, but I rather think I prefer your approach. It avoids the need for handling the event fired for deletions completely. Nice approach.
Jan 14 '13 #11

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

Similar topics

6
by: Amit Kela | last post by:
I am using ASP with SQL for my database. The problem I have is that even after I have ordered certain items from the shopping cart table on the webpage, I cannot remove them - that is the entire...
1
by: Mark | last post by:
This question refers to a main form with a continuous form subform. After an error occurs after entering several records in the subform, how can I delete all the data in the main form and all the...
3
by: Nathan Bloom | last post by:
Hi, I have a data entry form (access 2000) that also allows the user to add, update, and delete records from the form. The Delete action is carried out in an event procedure and has the...
2
by: uv | last post by:
Hi! I'm having problems submitting a new record through the form. I'm working with the wizard and I've added a control button to my form for entering entering a new record but for some reason it...
1
by: Coy Howe | last post by:
This one seems bizarre! We have a database consisting of a main table and 12 - 15 "sub" tables, which are connected via cascading relationships. The database performs many complex calculations...
1
by: KC | last post by:
Hello, I am using Access 2002. WinXP, Template from MS called Orders Mgmt DB. I have tweaked this DB to work for our small co. It has worked pretty well up until I made the mistake of deleting...
1
by: KC | last post by:
Hello, I am using Access 2002. WinXP, Template from MS called Orders Mgmt DB. I have tweaked this DB to work for our small co. It has worked pretty well up until I made the mistake of deleting...
2
by: padmaneha | last post by:
Hi I have created two tables 'TrainsMaster' & 'TransArrvlDepinfo' Columns which I have created in 'TrainsMaster' are 'trainName,TrainNo, StartStaionId, & EndstationId' Columns which I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.