473,385 Members | 1,535 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,385 software developers and data experts.

Delete button not asking if the user wants to delete record first

I used the button wizard to set up a delete record button. It does delete a record and then moves to the next record, however I need it to ask the user first before deleting and also if it is the last record I need it to return the previous record.
I'm not real familier with working with macros.
Is this an easy fix or should this be in code instead?

v/r,
jason
Apr 15 '13 #1
13 3375
Seth Schrock
2,965 Expert 2GB
Most of us in this forum use VBA for everything, so most solutions will probably use VBA.

What I would do is to use the MsgBox function to ask the user if they want to delete the record. If yes, then use the Recordset.Delete method to delete the record and then do a MovePrevious to go the previous record.
Apr 15 '13 #2
Ok so, I've put together this code but wanted you to look at it so I don't delete something I shouldn't...like my whole form or something.

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdDelete_Click()
  2. If Me.Currentrecord = Me.Recordset.Recordcount then
  3. Msgbox "Are you sure you want to delete this record?", vbCritical, "Deleting Record", vbYesNo
  4. If vbNo then
  5. DoCmd.CancelEvent
  6. Else
  7. If vbYes then
  8. Recordset.Delete
  9. DoCmd.GoToRecord, ,acPrevious
  10. End If
  11. End If
  12. End Sub
  13.  
Apr 15 '13 #3
NeoPa
32,556 Expert Mod 16PB
That seems like a good question to ask Jason. Here is an alternative version of your code with some comments explaining some of it and the differences :

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdDelete_Click()
  2.     Dim blnLast As Boolean
  3.  
  4.     'MsgBox call must return the value in order to be checked.
  5.     'If user says no then cancelling is not required.  It is only required 
  6.     '  that the deletion command is not invoked.
  7.     If MsgBox(Prompt:="Are you sure you want to delete this record?" _
  8.             , Buttons:=vbYesNo Or vbQuestion _
  9.             , Title:="Deleting Record") = vbNo Then Exit Sub
  10.     'At this point it's clear we need to delete the record.
  11.     'Using With Me is a more efficient approach to object usage.
  12.     With Me
  13.         'Remember for later if record is last.
  14.         blnLast = (.CurrentRecord = .Recordset.RecordCount)
  15.         Call .Recordset.Delete
  16.         'Only step back if deleted record was the last.
  17.         If blnLast Then Call DoCmd.GoToRecord(Record:=acPrevious)
  18.     End With
  19. End Sub
The MsgBox() call uses named parameters in order to make understanding the code easier. Multi-parameter functions are easy to be confused by if only the position of the parameters is there to indicate what is what. I recommend you look into the possibilities here. The Buttons parameter at least can be fiddly as it uses bitwise OR operations on the various possible values. Not easy logic to grasp.
Apr 15 '13 #4
Seth Schrock
2,965 Expert 2GB
@NeoPa Just curious, would there be a difference in line 17 between using the DoCmd.GoToRecord... vs .Recordset.MovePrevious? The later seems simpler, especially since you already have it in the With statement.

Also, I just want to clarify something. I believed that Me.Recordset.Delete deleted the current record, but in verifying this to make sure the wrong records wouldn't be deleted, I found the following post in another forum that said you have to tell the recordset which record you want to delete. Unfortunately, MSDN says nothing about the Delete method for versions 2007 or 2010 (I haven't looked for 2003), and I couldn't find anything else online to confirm one side or another. I see you have used the Delete method, so I'm assuming that I was correct the first time.
Apr 15 '13 #5
ADezii
8,834 Expert 8TB
As a side note, if you are going to use this approach you must make sure that the User cannot use the Menu Bar or any other Method to Delete a Record. Should this happen, your Code will never be executed.
Apr 15 '13 #6
NeoPa
32,556 Expert Mod 16PB
Seth:
@NeoPa Just curious, would there be a difference in line 17 between using the DoCmd.GoToRecord... vs .Recordset.MovePrevious? The later seems simpler, especially since you already have it in the With statement.
Very possibly Seth. My aim in providing the code was to illustrate the basic logic and the flow of the code. I didn't really bother about the actual job being done. The original code illustrated some lack of understanding of the flow I felt, so I wanted to illustrate what was doing what and and when each part should be included. Also, to provide a structure for the code that was solid and reliable, to be built on further if required.
Apr 16 '13 #7
Ok, I used the following code and got an error saying End With without with.

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdDelete_Click()
  2.  
  3. Dim blnLast As Boolean
  4.  
  5. 'Msgbox call must return the value in order to be checked
  6. If MsgBox("Are you sure you want to delete this record?", vbYesNo, "Deleting Record") Then
  7. If vbNo then
  8. Exit sub
  9.  
  10. With me
  11.   blnLast=(.CurrentRecord=.Recordset.RecordCount)
  12.   Call .Recordset.Delete
  13.   If blnLast Then
  14.   DoCmd.GoToRecord, ,acPrevious
  15.  End With
  16. End Sub
Apr 16 '13 #8
Rabbit
12,516 Expert Mod 8TB
You're missing an End If.
Apr 16 '13 #9
I've added the End If. Now there are no errors but it is also not deleting the record.

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdDelete_Click() 
  2.  
  3. Dim blnLast As Boolean 
  4.  
  5. 'Msgbox call must return the value in order to be checked 
  6. If MsgBox("Are you sure you want to delete this record?", vbYesNo, "Deleting Record") = vbNo then 
  7. Exit sub 
  8.  
  9. With me 
  10.   blnLast=(.CurrentRecord=.Recordset.RecordCount) 
  11.   Call .Recordset.Delete 
  12.   If blnLast Then 
  13.   DoCmd.GoToRecord, ,acPrevious 
  14. End If
  15.  End With
  16. End If 
  17. End Sub 
  18.  
Apr 16 '13 #10
Seth Schrock
2,965 Expert 2GB
Try doing a requery (or just close the form and reopen it) to make sure that the form is updated with the latest data. I didn't think that this was necessary, but no harm checking.
Apr 16 '13 #11
adding a requrey takes it back to the 1st record if the user clicks yes, instead of going to the previous record. And, it doesn't delete the record.
Apr 16 '13 #12
Rabbit
12,516 Expert Mod 8TB
Your End If for your first If statement is in the wrong place. Move line 16 to line 8.
Apr 16 '13 #13
NeoPa
32,556 Expert Mod 16PB
Jason, you've changed both instances of my single-line If statements into multi-line ones, but without converting it to the multi-line format. If it goes across multiple lines, and without continuation characters, it's the multi-line format and requires an End If.
Apr 17 '13 #14

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

Similar topics

3
by: Steve Lefevre | last post by:
Hey folks - I have a form for scanning in packages as they arrive. It's a continuous form, with one field to hold the scanned case number. On the row I also have a button to delete the record,...
3
by: vcornjamb | last post by:
Hello, I am developing a web form that contains some buttons and a data grid which has as its last column link buttons that will delete the data associated with that row. Everything works fine,...
3
by: NateDawg | last post by:
I'm reposting this. I'm kinda in a bind untill i get this figured out, so if anyone has some input it would sure help me out. Ok, I’ve noticed a few gridview problems floating around the forum....
2
by: jim Bob | last post by:
Hi, I have a form with a list box that shows the contents of a table and managed to create an add record button with the wizard. (DoCmd.GoToRecord , , acNewRec) Now i want to make a delete...
5
by: Jeff User | last post by:
Hello ..NET 1.1, VS 2003, C# & asp.net I have tried to follow msdn instructions and samples but I can not get an event to fire for this button on the datagrid. There has to be something obvious...
1
by: JasonK | last post by:
I would like to move the Delete button such that it displays one time in the footer row, rather than on every row. I've seen lots of questions asked on the subject around the net, but no answer...
5
by: Gareith Stanley | last post by:
Hi there I have an access form that allows the user to browse records (< and > buttons) sorted by another field within that table (eg, all entries under a staff group, all entries under a student...
3
by: bluez | last post by:
I want to design a webpage where user can search the data from the database and list out the related records. Each of the record got a delete button which allow user to delete the record. ...
7
by: ITAutobot25 | last post by:
My delete button is not working in my GUI and my due date is today before midnight. Can anyone show me how to correct this error? My assignment statement is below as well as 5 classes. InventoryGUI...
1
by: faisalabad | last post by:
Hi everyone newb, i am connecting to database then accessing a table and printing whole table on php page as table with "delete" button at the end of every row (so same delete button being printed...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.