473,387 Members | 1,864 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,387 software developers and data experts.

Deleting multiple tables misses some each iteration

gnawoncents
214 100+
Greetings.
I have a split Access 2013 database (back end with tables and front end with everything else) an am having trouble with deleting tables.

I run the code below to check the back end for only tables that meet my required criteria then delete them from the back end and delete the links from the front end.

When I test the code using a simple msgbox line naming the tables that will be deleted, it iterates through all the appropriate tables; however, when I run the actual code, only two or three tables are deleted at a time (with no popup errors). If I run the code again, a few more are deleted, and I can continue running it until all the correct tables are deleted.

Why won't it delete all the tables in one go?

I suspect it is some sort of a timing issue, but am not sure. Ideas anyone? Thanks for your help.

Expand|Select|Wrap|Line Numbers
  1. Private Sub btnDeleteTables_Click()
  2.  
  3. '---------------------------------------------------------------------------------------------
  4. 'Find all past and future-dated (not current month) schedule tables not in use then
  5. 'delete them from the back end and remove the links in the front end
  6. '---------------------------------------------------------------------------------------------
  7.  
  8. Dim dbDEL As Database
  9. Dim tdfDEL As TableDef
  10. Dim strTable As String
  11. strTable = ""
  12.  
  13. 'Get the location of the back end
  14. Dim strBackEndPath As String
  15. Dim lenPath As Integer
  16. Dim i As Integer
  17. Dim j As Integer
  18.  
  19. strBackEndPath = CurrentDb.TableDefs("tbScheduleALL").Connect
  20.  
  21. 'Remove the datebase & password prefix
  22. j = InStrRev(strBackEndPath, "=") + 1
  23. strBackEndPath = Mid(strBackEndPath, j)
  24.  
  25. Set dbDEL = OpenDatabase(strBackEndPath)
  26.  
  27. For Each tdfDEL In dbDEL.TableDefs
  28.     strTable = tdfDEL.Name
  29.     'Check to see if the table is a schedule table, linked, and not marked as in use (fldActive <> -1)
  30.     If (strTable Like "tb######*") And strTable <> "tb" & Format(Date, "yyyymm") Then
  31.         If Len(CurrentDb.TableDefs(strTable).Connect) > 0 Then
  32.             If (DLookup("[fldActive]", strTable) <> -1) Then
  33.                 dbDEL.TableDefs.Delete strTable 'delete table from the back end
  34.                 DoCmd.DeleteObject acTable, strTable 'Remove link to deleted tables from the front end
  35.             End If
  36.         End If
  37.     End If
  38. Next
  39.  
  40. dbDEL.Close
  41. Set dbDEL = Nothing
  42. Set tdfDEL = Nothing
  43.  
  44. End Sub
  45.  
Dec 28 '16 #1

✓ answered by jforbes

I have a feeling that your iterator (For Each Loop) is not aware that tableDefs are being Deleted. So when one is deleted, either the iterator gets lost, or it skips what would become the new current one (after the current one is deleted), effectively skipping the one right after the one that is deleted.

You might be able to re-write you loop to a Do While and reference the TableDefs by Index.

4 1201
jforbes
1,107 Expert 1GB
I have a feeling that your iterator (For Each Loop) is not aware that tableDefs are being Deleted. So when one is deleted, either the iterator gets lost, or it skips what would become the new current one (after the current one is deleted), effectively skipping the one right after the one that is deleted.

You might be able to re-write you loop to a Do While and reference the TableDefs by Index.
Dec 29 '16 #2
gnawoncents
214 100+
Thanks--I'll give that a go!
Dec 29 '16 #3
gnawoncents
214 100+
Many thanks, jforbes, you were right on the mark. Instead of deleting the tables in my For Each Loop, I created and added them to a collection, then iterated through the collection once complete. It works perfectly. You are awesome!
Dec 29 '16 #4
NeoPa
32,556 Expert Mod 16PB
That's absolutely correct.

Every time a TableDef is removed from the collection any previous pointer, or object set, to that collection remains pointing to the original collection and is not equivalent to a pointer/object set to the collection after one is deleted. The original keeps the same dimensions as before but simply sets the place for deleted entries to a marker indicating it no longer exists.

I added two new tables to my database and ran the following code (All on one line in the Debug Pane but I show it here separated out for ease of reading) :
Expand|Select|Wrap|Line Numbers
  1. Set TS=CurrentDb().TableDefs
  2. ?TS.Count;CurrentDb().TableDefs.Count;"|";
  3. Call DoCmd.DeleteObject(acTable,"tblUpdate1")
  4. ?TS.Count;CurrentDb().TableDefs.Count;"|";
  5. Call DoCmd.DeleteObject(acTable,"tblToDelete1")
  6. ?TS.Count;CurrentDb().TableDefs.Count;
The results I got were :
Expand|Select|Wrap|Line Numbers
  1.  93  93 | 93  92 | 93  91
You'll notice the TS object doesn't get reduced but continues to 'think' it's covering 93 tables even after two have been deleted.

In case you're wondering, adding tables invalidates the object entirely. Had you tried that your code would have crashed instead.

@Nonsense.
Your solution is a good one. You should also have been able to remove them in order from the original as long as you went backwards through the collection. That would involve accessing it numerically though, which is less intuitive than using a string reference.

Another thing to consider, assuming you only ever want today's one at any one time, is to rename and repoint the single link to ensure it points to the correct table. That might get complicated though as you'd need to identify the table to rename by something other than its name of course. All just ideas.
Jan 6 '17 #5

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

Similar topics

8
by: Rigga | last post by:
Hi, I am new to mysql and need help on how to join tables. I have a database which contains 4 tables, the main table contains information by date order and the other 3 contain data also in date...
2
by: IanCraft | last post by:
I am new to Access 2002 and have a questions regarding using a form for data entry. I have 15 items I need to keep track of that are listed individually on a form as follows: Item#1Name ...
11
by: dskillingstad | last post by:
I've been struggling with this problem for some time and have tried multiple solutions with no luck. Let me start with, I'm a novice at Access and I'm not looking for someones help to design my...
13
by: ricky.agrawal | last post by:
I'm really not sure how to go about this in Access. What I've created is a table for each location. Those tables are identical in format but different in information. The tables are named after...
6
by: ApexData | last post by:
I have 2 tables: Table1 and Table2. Neither one has a primary key because each table will only have 1-record. My form is a SingleForm unbound with tabs (my desire here). Using this form, in...
0
by: Bo | last post by:
I've got a XML file like the example below which I would like to import into a mysql database. I would like to import into multiple tables so each Channel Name gets its own table. How would I go...
8
by: beretta819 | last post by:
Ok, so I apologize in advance for the wordiness of what follows... (I am not looking for someone to make this for me, but to point me in the right direction for the steps I need to take.) I was...
7
by: orderofblessing | last post by:
Hi every one: I just registered on forum today to get the help on a little but tricky problem to me. Actually i am converting a php website into .NET C#. I just want to display each search...
2
by: doverj | last post by:
Is there a way to delete from multiple tables/views a column with a specific name? For example, a database has 50 tables and 25 views all have a column named ColumnA. Is it possible to write a...
3
Midzie
by: Midzie | last post by:
Hi All, first of all please don't delete my post... I need some answers on it that's why I posted a question here. If anyone there could help me please, I am deleting records from multiple tables but...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...

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.