472,356 Members | 1,961 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,356 software developers and data experts.

Loop to delete record(s) in multiple tables Access 2003

93
I have a database with over 20 tables. I need to delete one to many record(s) in all tables by using a loop code. Some tables with have one record, multiple records or no records for the criteria given. I can not do cascade delete because there are to many tables and I don't want to write 20 plus delete queries. Does anyone have any ideas how to accomplish this.
Mar 6 '08 #1
6 4019
epots9
1,351 Expert 1GB
Moved to the Access Forums where the resident experts can better assist you.

**Moved from Programming Challenges.
Mar 6 '08 #2
I'm new here... I answered the wrong question and I am trying to delete this post
Mar 6 '08 #3
I have a database with over 20 tables. I need to delete one to many record(s) in all tables by using a loop code. Some tables with have one record, multiple records or no records for the criteria given. I can not do cascade delete because there are to many tables and I don't want to write 20 plus delete queries. Does anyone have any ideas how to accomplish this.
Ok clloyd, maybe I didn't answer the wrong question yesterday. When you say "one to many records," are you referring to a relationship between the tables or the quantity of records you are wanting to delete in each table? Could you please post just a little more information? I may have a simple answer for you.
Mar 7 '08 #4
clloyd
93
Ok clloyd, maybe I didn't answer the wrong question yesterday. When you say "one to many records," are you referring to a relationship between the tables or the quantity of records you are wanting to delete in each table? Could you please post just a little more information? I may have a simple answer for you.
Sorry I was on other projects. In all the tables there is a client number that is the the relationship. In some tables there will be only one record for that client while in others there could be 50 records. When a I need to close a client record I want to delete everything in that database connected to that unique client number. Thanks
Mar 25 '08 #5
Stewart Ross
2,545 Expert Mod 2GB
Hi. What you ask is possible, though not easy to achieve. Could I ask you to consider why you should delete such data at all?

It is not normal practice with relational databases to deliberately and permanently remove customer records from a database when the account is no longer active. Losing data like this means you will never be able to answer queries such as 'how many customers are no longer active with our business'; or 'how many customers have closed their accounts with us in the past year', say. You will not be able to mailshot these clients, or send advertising fliers out saying how much you would welcome their business.

A different approach which I take myself in a Human Resources context for personnel who have left the business is to include a status field in the person record - the client record in your case. A simple boolean called 'current' would do, along with an exit date to show when the person's account was closed. You change current to False for customers who are no longer active, and populate the leaving date accordingly.

To make sure you only view active customers in your main forms and reports you would add the Status field to the underlying queries and select only the currently-active records for display or reporting.

The advantage of doing so is that once all the changes are made you only need to modify two fields in one form to record a customer as no longer active. You would continue to hold the customer records, but this is no disadvantage at all. As the customer record is involved in all your main one-to-many relationships, including the status criteria of selecting only Current records will make sure that you do not see inactive customer records, without doing any deletions at all.

Although this does not answer the question you posed, which is all about programming multiple deletes, I would urge you to consider what I have suggested above. Once data is deleted there is no going back, and personally I think the value of the lost data (even from accounts which are closed) far outweighs the minor inconvenience of classifying accounts as inactive and continuing to carry the records. As an aside, it will also ensure that there is no attempt made to re-use the unique customer ID associated with each customer.
Please consider what I have suggested above.

If you really want to pursue the deletion aspect it is possible, but Access does not provide multi-table delete facilities in SQL. This would lead to processing multiple deletes in VB code loops. Easy enough to achieve if you have a list of the table names whose records are to be deleted, stored in a temporary table say. The loop would simply process one table name after another and run a delete for each. Sample skeleton VB code for this is provided below.

Expand|Select|Wrap|Line Numbers
  1. Dim RS as DAO.RecordSet
  2. Dim strTableName as String
  3. Dim strSQL as String
  4. ' Open temporary table storing table names of tables
  5. ' to delete records with particular customer IDs
  6. ' Must be ordered by innermost (many side) relationships first - don't attempt
  7. ' to delete records on the One side of a relationship when there exist 
  8. ' many side relationships
  9. Set RS = CurrentDB.OpenRecordset("temporary table")
  10. DoCmd.Setwarnings False
  11. Do while not RS.EOF
  12.     strTableName = RS![tablename field]
  13.     strSQL = "DELETE * from " & strTableName & " WHERE [your customer ID field] = " & [Your customer ID] & ";"
  14.     DoCmd.RunSQL(strSQL)
  15.     RS.Movenext
  16. loop
  17. DoCmd.Setwarnings True
  18. RS.Close
  19.  
-Stewart
Mar 25 '08 #6
clloyd
93
Hi. What you ask is possible, though not easy to achieve. Could I ask you to consider why you should delete such data at all?

It is not normal practice with relational databases to deliberately and permanently remove customer records from a database when the account is no longer active. Losing data like this means you will never be able to answer queries such as 'how many customers are no longer active with our business'; or 'how many customers have closed their accounts with us in the past year', say. You will not be able to mailshot these clients, or send advertising fliers out saying how much you would welcome their business.

A different approach which I take myself in a Human Resources context for personnel who have left the business is to include a status field in the person record - the client record in your case. A simple boolean called 'current' would do, along with an exit date to show when the person's account was closed. You change current to False for customers who are no longer active, and populate the leaving date accordingly.

To make sure you only view active customers in your main forms and reports you would add the Status field to the underlying queries and select only the currently-active records for display or reporting.

The advantage of doing so is that once all the changes are made you only need to modify two fields in one form to record a customer as no longer active. You would continue to hold the customer records, but this is no disadvantage at all. As the customer record is involved in all your main one-to-many relationships, including the status criteria of selecting only Current records will make sure that you do not see inactive customer records, without doing any deletions at all.

Although this does not answer the question you posed, which is all about programming multiple deletes, I would urge you to consider what I have suggested above. Once data is deleted there is no going back, and personally I think the value of the lost data (even from accounts which are closed) far outweighs the minor inconvenience of classifying accounts as inactive and continuing to carry the records. As an aside, it will also ensure that there is no attempt made to re-use the unique customer ID associated with each customer.
Please consider what I have suggested above.

If you really want to pursue the deletion aspect it is possible, but Access does not provide multi-table delete facilities in SQL. This would lead to processing multiple deletes in VB code loops. Easy enough to achieve if you have a list of the table names whose records are to be deleted, stored in a temporary table say. The loop would simply process one table name after another and run a delete for each. Sample skeleton VB code for this is provided below.

Expand|Select|Wrap|Line Numbers
  1. Dim RS as DAO.RecordSet
  2. Dim strTableName as String
  3. Dim strSQL as String
  4. ' Open temporary table storing table names of tables
  5. ' to delete records with particular customer IDs
  6. ' Must be ordered by innermost (many side) relationships first - don't attempt
  7. ' to delete records on the One side of a relationship when there exist 
  8. ' many side relationships
  9. Set RS = CurrentDB.OpenRecordset("temporary table")
  10. DoCmd.Setwarnings False
  11. Do while not RS.EOF
  12.     strTableName = RS![tablename field]
  13.     strSQL = "DELETE * from " & strTableName & " WHERE [your customer ID field] = " & [Your customer ID] & ";"
  14.     DoCmd.RunSQL(strSQL)
  15.     RS.Movenext
  16. loop
  17. DoCmd.Setwarnings True
  18. RS.Close
  19.  
-Stewart

Thank you for the suggestion. This is an odd Database in that it is a current picture so to speak. We don't keep historical data for this database. If a location were to come back we would re-enter everything new as there could be a lot of differences. It is however rare that that happens. Thank you however for the above code.
Mar 25 '08 #7

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

Similar topics

12
by: jason | last post by:
Access 2000: I have a customer-inventory table I need to loop through and compile a list of all the inventory items the customer is tracking. The problem I am finding is that a simple loop...
16
by: Philip Boonzaaier | last post by:
I want to be able to generate SQL statements that will go through a list of data, effectively row by row, enquire on the database if this exists in the selected table- If it exists, then the colums...
4
by: barbara_dave | last post by:
Hi all, We have a Access database used by multiple user. For some reason the database get a "Error" record. When doing search or record pointer point to this record, we get run time error...
3
by: Tim Marshall | last post by:
HI all, Access 2003, Jet back end. Rather than annoy my users in a particular app by having relationships with enforced relational integrity refuse to delete a record with related records, I'm...
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...
3
by: Jeff | last post by:
In a subform I have a simple SQL statement that links an order details table to a product table. The form is used to enter order details. Most fields are obviously from the details table, with only...
1
by: jpr | last post by:
Hello, My database has 5 tables. WHen I add data to one table, it runs an append query that copies three records to other 4 tables. The main table is MASTER. The data I copy are: ID, SSN and...
5
by: Bob Bridges | last post by:
Start with two tables, parent records in one and child records in the other, a one-to-many relationship. Create a select statement joining the two. Display the query in datasheet mode. When I...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
1
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. header("Location:".$urlback); Is this the right layout the...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it so the python app could use a http request to get...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...

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.