363,927 Members | 2741 Browsing Online
Community for Developers & IT Professionals
Bytes IT Community

How to Loop Delete on a DAO Recordset

kcdoell
100+
P: 230
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
Share this Question
Share on Google+
2 Replies


kcdoell
100+
P: 230
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
P: 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

Post your reply

Help answer this question



Didn't find the answer to your Microsoft Access / VBA question?

You can also browse similar questions: Microsoft Access / VBA