473,654 Members | 3,104 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to Loop Delete on a DAO Recordset

kcdoell
230 New Member
Hello I have a code where I want to delete the records that are found in my DAO recordset. I took a stab at this for the first time and got it to work but it is only delete one record at a time. If I execute the code again my record count will be minus one and then it will delete another single record etc etc until there are no records to delete. How could I create a loop statement so that I don't have to keep on executing the code??? Below is what I have so far:

Expand|Select|Wrap|Line Numbers
  1. 'Procdure to give the user the ability to delete all records
  2. 'for a predefined recordset from the tblStaticAllForecast table
  3.  
  4.         LockSQL = "SELECT * FROM tblStaticAllForecast WHERE" & _
  5.                     " DivisionIDFK = " & Val(Me.cboDivision.Value) & _
  6.                     " And WrkRegIDFK = " & Val(Me.cboWrkReg.Value) & _
  7.                     " And CreditRegIDFK = " & Val(Me.cboCreditReg.Value) & _
  8.                     " And YearID = " & Val(Me.CboYear.Value) & _
  9.                     " And MonthID = " & Val(Me.CboMonth.Value) & _
  10.                     " And FWeek = " & Val(Me.cboWeek.Value)
  11.  
  12. Dim rst As DAO.Recordset
  13. Set rst = CurrentDb.OpenRecordset(LockSQL)
  14.  
  15. 'Check to see if there are any records
  16.  
  17.     If rst.BOF And rst.EOF Then 'If none, then end process and send out MsgBox
  18.  
  19.         MsgBox "There are no records to delete.", 64, "No Records Match"
  20.  
  21.      Else
  22.  
  23. 'Find the last and first record for the count
  24.  
  25.         rst.MoveLast 'Move to last record
  26.         rst.MoveFirst 'Move to First record
  27.  
  28. 'Count the records found in "LockSQL"
  29.  
  30.         recordexists = rst.RecordCount
  31.  
  32.   If MsgBox("The number of records you are about to delete is " & recordexists & "." & _
  33.             " Click the ok button to proceed", vbOKCancel, vbDefaultButton2) = vbOK Then
  34.  
  35. 'Delete the records that the user has selected.
  36.  
  37.     rst.Delete
  38.  
  39.         MsgBox "Records have been deleted.", vbInformation, "Message"
  40.  
  41. 'Close the recordset
  42.  
  43.     rst.Close
  44.  
  45.   End If
  46.         End If
  47.             End If
  48. End Sub
  49.  
Thanks,


Keith.
Mar 28 '08 #1
2 15119
kcdoell
230 New Member
I figured this one out with a loop statement....

Expand|Select|Wrap|Line Numbers
  1. 'Delete the records that the user has selected.
  2.  
  3.     With rst
  4.  
  5.         .Delete
  6.  
  7.     End With
  8.  
  9. 'Check to make sure that at least one record exists in the recordsert
  10.  
  11. If (rst.RecordCount > 0) Then
  12.  
  13.     rst.MoveFirst ' Start deletion from first record
  14.  
  15. 'Delete one record at a time using a do while loop
  16.  
  17.          Do While Not rst.EOF
  18.             rst.Delete
  19.             rst.MoveNext
  20.          Loop
  21.     End If
  22.  
  23.     MsgBox "Records have been deleted.", vbInformation, "Message"
  24.  
  25.  
Thanks
Mar 28 '08 #2
davidelloyd
2 New Member
I have a additonal question. I would like to delete on record but the first example seems a bit complicated do you have a code that will delete one record in a table that looks for certain words in the field names.
Aug 18 '08 #3

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

Similar topics

0
1959
by: Mike | last post by:
I'm having a problem with the grid not getting refreshed after deleting a record. Even though the record is deleted from the table. I have the following code in a Delete button event: sql = "Delete from OrderDetails where Orderid = " & datOrderItems.Recordset!OrderId sql = sql & " and ProductId = '" & datOrderItems.Recordset!ProductId & "'" gconn.Execute sql datOrderItems.Recordset.Requery datOrderItems.Recordset.MoveNext
8
1690
by: Drew | last post by:
I am trying to build a small app that shows a Course Title from the database, then displays a dropdown full of categories for the user to choose one... I thought a loop would be the best way to accomplish this, but since the dropdown is dynamic, I am having problems. Can someone help me out here? Here is my code, For i = 1 to Num
5
25077
by: !TG | last post by:
I currently use Do while loop, but I'd rather use a For Loop though I have never gotten the hang of them. Would some one please be so kind as to show me how to loop through a recordset.
7
2862
by: David Mitchell | last post by:
I use a function to read all of the files from a couple of directories (and subfolders) and update a table(tblfiles) with the fullpath and file name, the filesize and the date the file was created. I then manually select files to delete by clicking on a yes/no checkbox. I have a query (qryfiledelete) which selects all of the files from tblfiles where the yes/no field is yes. What I want to do is run a function which will delete (Kill?)...
5
25450
by: tony010409020622 | last post by:
I just spent 4 months taking a dotnet class where i learned very little. One of the things I did not learn is this: What are the dotnet equivilents of commands such as: Adodc1.Recordset.AddNew Adodc1.Recordset.Update Adodc1.Recordset.MoveFirst Adodc1.Recordset.MoveNext Adodc1.Recordset.Delete
52
6312
by: MP | last post by:
Hi trying to begin to learn database using vb6, ado/adox, mdb format, sql (not using access...just mdb format via ado) i need to group the values of multiple fields - get their possible variations(combination of fields), - then act on each group in some way ...eg ProcessRs (oRs as RecordSet)... the following query will get me the distinct groups
1
6097
by: hmiller | last post by:
Hey Guys, I'm just playing around with some code I wrote for work and am trying to minimize the coding as much as possible. So two questions: 1. What are your best practices for organizing your code, aside from comments? I typically split my code up into sub procedures and then run them all from the top of the code. My problem is that my code page is
4
2487
by: jasone | last post by:
Hi ive got checkbox options going to a page and then being listed, i now want to delete what has been selected, im guessing this needs to be done through some kind of loop? the code i have so far on the delete page is as follows : <% Dim flight_id flight_id = Request.Form("flight_id") if flight_id="" then
2
3807
by: farouqdin | last post by:
Hi all i have code which loops through table and deletes the duplicate records. This code does it for one table. How do i change it so it goes through several tables? On Error Resume Next Dim db As DAO.Database, rst As DAO.Recordset Dim strDupName As String, strSaveName As String Set db = CurrentDb()
0
8816
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8494
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8596
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5627
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4297
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2719
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1924
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1597
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.