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
13 3375
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.
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. -
Private Sub cmdDelete_Click()
-
If Me.Currentrecord = Me.Recordset.Recordcount then
-
Msgbox "Are you sure you want to delete this record?", vbCritical, "Deleting Record", vbYesNo
-
If vbNo then
-
DoCmd.CancelEvent
-
Else
-
If vbYes then
-
Recordset.Delete
-
DoCmd.GoToRecord, ,acPrevious
-
End If
-
End If
-
End Sub
-
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 : - Private Sub cmdDelete_Click()
-
Dim blnLast As Boolean
-
-
'MsgBox call must return the value in order to be checked.
-
'If user says no then cancelling is not required. It is only required
-
' that the deletion command is not invoked.
-
If MsgBox(Prompt:="Are you sure you want to delete this record?" _
-
, Buttons:=vbYesNo Or vbQuestion _
-
, Title:="Deleting Record") = vbNo Then Exit Sub
-
'At this point it's clear we need to delete the record.
-
'Using With Me is a more efficient approach to object usage.
-
With Me
-
'Remember for later if record is last.
-
blnLast = (.CurrentRecord = .Recordset.RecordCount)
-
Call .Recordset.Delete
-
'Only step back if deleted record was the last.
-
If blnLast Then Call DoCmd.GoToRecord(Record:=acPrevious)
-
End With
-
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.
@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.
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.
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.
Ok, I used the following code and got an error saying End With without with. -
Private Sub cmdDelete_Click()
-
-
Dim blnLast As Boolean
-
-
'Msgbox call must return the value in order to be checked
-
If MsgBox("Are you sure you want to delete this record?", vbYesNo, "Deleting Record") Then
-
If vbNo then
-
Exit sub
-
-
With me
-
blnLast=(.CurrentRecord=.Recordset.RecordCount)
-
Call .Recordset.Delete
-
If blnLast Then
-
DoCmd.GoToRecord, ,acPrevious
-
End With
-
End Sub
You're missing an End If.
I've added the End If. Now there are no errors but it is also not deleting the record. -
Private Sub cmdDelete_Click()
-
-
Dim blnLast As Boolean
-
-
'Msgbox call must return the value in order to be checked
-
If MsgBox("Are you sure you want to delete this record?", vbYesNo, "Deleting Record") = vbNo then
-
Exit sub
-
-
With me
-
blnLast=(.CurrentRecord=.Recordset.RecordCount)
-
Call .Recordset.Delete
-
If blnLast Then
-
DoCmd.GoToRecord, ,acPrevious
-
End If
-
End With
-
End If
-
End Sub
-
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.
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.
Your End If for your first If statement is in the wrong place. Move line 16 to line 8.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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,...
|
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,...
|
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....
|
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...
|
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...
|
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...
|
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...
|
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.
...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
| |