473,399 Members | 2,774 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,399 software developers and data experts.

How to Loop Delete on a DAO Recordset

kcdoell
230 100+
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 15073
kcdoell
230 100+
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
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
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 =...
8
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...
5
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
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....
5
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...
52
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...
1
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...
4
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...
2
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...
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: 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: 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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.