473,651 Members | 3,093 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to delete all records found in a DAO recordset

kcdoell
230 New Member
Hello:

I am trying to write a code that will delete all records found in my DAO recordset Below is the code 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. recordexists = rst.RecordCount
  15.  
  16. 'If no records are found
  17.     If recordexists = 0 Then
  18.         MsgBox "There are no records to delete."
  19.             Else
  20.  
  21.     rst.MoveLast        'Move to last record
  22.     rst.MoveFirst        'Move to First record
  23.  
  24.         If MsgBox("The number of records you are about to delete is " & recordexists & "." & _
  25.         " Click the ok button to proceed", vbOKCancel, vbDefaultButton2) = vbOK Then
  26.  
  27. 'code will delete the records that the user has selected.
  28.  
  29.  
  30.    End If
  31.       End If
  32.  
  33. End Sub
  34.  
  35.  
Is there a quick code that would do this?

Keith.
Mar 27 '08 #1
5 14356
PianoMan64
374 Recognized Expert Contributor
Hello:

I am trying to write a code that will delete all records found in my DAO recordset Below is the code 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. recordexists = rst.RecordCount
  15.  
  16. 'If no records are found
  17.     If recordexists = 0 Then
  18.         MsgBox "There are no records to delete."
  19.             Else
  20.  
  21.     rst.MoveLast        'Move to last record
  22.     rst.MoveFirst        'Move to First record
  23.  
  24.         If MsgBox("The number of records you are about to delete is " & recordexists & "." & _
  25.         " Click the ok button to proceed", vbOKCancel, vbDefaultButton2) = vbOK Then
  26.  
  27. 'code will delete the records that the user has selected.
  28.  
  29.  
  30.    End If
  31.       End If
  32.  
  33. End Sub
  34.  
  35.  
Is there a quick code that would do this?

Keith.
Are you simply wanting to delete all Records within the criteria of what you're deleting?

If so, once you established the connection, you can simply add a SQL Statement that will delete all the records that have been selected from you're conditional statement.

example

Expand|Select|Wrap|Line Numbers
  1.  
  2. DoCmd.RunSQL ("DELETE * FROM tblStaticAllForecast WHERE" & _
  3.         " DivisionIDFK = " & Val(Me.cboDivision.Value) & _
  4.         " And WrkRegIDFK = " & Val(Me.cboWrkReg.Value) & _
  5.         " And CreditRegIDFK = " & Val(Me.cboCreditReg.Value) & _
  6.         " And YearID = " & Val(Me.CboYear.Value) & _
  7.         " And MonthID = " & Val(Me.CboMonth.Value) & _
  8.         " And FWeek = " & Val(Me.cboWeek.Value))
  9.  
Mar 29 '08 #2
ADezii
8,834 Recognized Expert Expert
Are you simply wanting to delete all Records within the criteria of what you're deleting?

If so, once you established the connection, you can simply add a SQL Statement that will delete all the records that have been selected from you're conditional statement.

example

Expand|Select|Wrap|Line Numbers
  1.  
  2. DoCmd.RunSQL ("DELETE * FROM tblStaticAllForecast WHERE" & _
  3.         " DivisionIDFK = " & Val(Me.cboDivision.Value) & _
  4.         " And WrkRegIDFK = " & Val(Me.cboWrkReg.Value) & _
  5.         " And CreditRegIDFK = " & Val(Me.cboCreditReg.Value) & _
  6.         " And YearID = " & Val(Me.CboYear.Value) & _
  7.         " And MonthID = " & Val(Me.CboMonth.Value) & _
  8.         " And FWeek = " & Val(Me.cboWeek.Value))
  9.  
Hello Pianoman, there is actually a much easier method:
Expand|Select|Wrap|Line Numbers
  1. DoCmd.SetWarnings False
  2.   DoCmd.RunSQL Replace(LockSQL, "Select", "Delete")
  3. DoCmd.SetWarnings True
Mar 29 '08 #3
kcdoell
230 New Member
I was away for a couple days and did not have access to the forum. I guess there is more than one way to skin a cat... Because I was in "Loop" mode, I came up with this solution:

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. 'Close the recordset
  26.      End If
  27.         End If
  28.             End If
  29. End Sub
  30.  
Thanks for the ideas and help. By the way, I like the cleaner look of the other ideas.....

Keith.
Apr 1 '08 #4
kcdoell
230 New Member
Hello Pianoman, there is actually a much easier method:
Expand|Select|Wrap|Line Numbers
  1. DoCmd.SetWarnings False
  2.   DoCmd.RunSQL Replace(LockSQL, "Select", "Delete")
  3. DoCmd.SetWarnings True
ADezii:

How does your method work. Is your code just simply swapping out the word "Select" for "Delete" in my SQL?

The reason I ask is that I am thinking of using it on something else I need to do. That is to say where the records have to append to a table; I could use the same method by swapping out the word "Select" for "Insert" in my SQL?

What do you think?

Keith.
Apr 1 '08 #5
ADezii
8,834 Recognized Expert Expert
ADezii:

How does your method work. Is your code just simply swapping out the word "Select" for "Delete" in my SQL?

The reason I ask is that I am thinking of using it on something else I need to do. That is to say where the records have to append to a table; I could use the same method by swapping out the word "Select" for "Insert" in my SQL?

What do you think?

Keith.
How does your method work. Is your code just simply swapping out the word "Select" for "Delete" in my SQL?
You hit the nail on the head, since the SQL is exactly the same except for these 2 Keywords, Replace() simply substitutes Delete for Select in the Statement.
Apr 1 '08 #6

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

Similar topics

8
4051
by: Steve | last post by:
I have several pairs of synchronized subforms in an application. I have a Delete button for each pair that uses the following code or similar to delete a record in the second subform: DoCmd.SetWarnings False DoCmd.RunCommand acCmdDeleteRecord DoCmd.SetWarnings True End If ExitHere: Me!SubName.SetFocus
5
5032
by: hpi | last post by:
Hello, I have a table : Batch It contains fields batchnummer : Number (Long Integer) datum : Date/Time status : Number (Long Integer) nr_records : Number (Long Integer)
13
3464
by: Jan | last post by:
Hi I have a database that I use to keep track of the sales promotions that we send to companies. I normally send a mailing based on a subset of the companies in the database (found using the query: QryICTMassDistribution3) , I then use a form and the code below to create a new record in the corrispondence table to show what corrispondence has been sent to various companies.
6
2102
by: paulwilliamsonremove | last post by:
Hi, I have two queries: "qryHistoryPersonIDs" that just contains the "personID" numeric field, and "qryDonations" that just contains the "personID" field, and a date field ("dDonationDate"). I want to delete every record in the qryHistoryPersonIDs recordset that are not found in the qryDonations recordset for a particular date range.
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
3
6126
by: igendreau | last post by:
I'm trying to clean up a database of mine, and I need to convert some old DAO code over to ADO. When I was using DAO, I had no problem running this script and deleting the record using rs.Delete. But when I rework the code in ADO, it tells me I can't delete it because there are related records in another table. Never had that problem in DAO and I haven't touched the relationships. Ignore most of this code and focus on the recordset "rs"....
4
8665
by: felicia | last post by:
Hi All, Below is my code to delete records: adodcAllEntries.Recordset.MoveFirst Do While (adodcAllEntries.Recordset.EOF = False) If adodcAllEntries.Recordset.Fields(0) = selected_id Then adodcAllEntries.Recordset.Delete End If
7
9863
by: AccessHunter | last post by:
I am using the following code to find and delete records in a table. The logic will go through each record and if a duplicate row is found will delete it. I ran this code and it worked the first time. Its not deleting the rows when I tried the second time. I debugged the code and its actually going through the delete step but the row is not getting deleted as it did the first time. Please help. Thanks in advance. Function...
2
15119
kcdoell
by: kcdoell | last post by:
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...
0
8703
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8467
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
8589
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...
1
6160
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5619
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
4145
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
4291
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2703
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
1591
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.