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
- Private Sub btnDeleteTables_Click()
- '---------------------------------------------------------------------------------------------
- 'Find all past and future-dated (not current month) schedule tables not in use then
- 'delete them from the back end and remove the links in the front end
- '---------------------------------------------------------------------------------------------
- Dim dbDEL As Database
- Dim tdfDEL As TableDef
- Dim strTable As String
- strTable = ""
- 'Get the location of the back end
- Dim strBackEndPath As String
- Dim lenPath As Integer
- Dim i As Integer
- Dim j As Integer
- strBackEndPath = CurrentDb.TableDefs("tbScheduleALL").Connect
- 'Remove the datebase & password prefix
- j = InStrRev(strBackEndPath, "=") + 1
- strBackEndPath = Mid(strBackEndPath, j)
- Set dbDEL = OpenDatabase(strBackEndPath)
- For Each tdfDEL In dbDEL.TableDefs
- strTable = tdfDEL.Name
- 'Check to see if the table is a schedule table, linked, and not marked as in use (fldActive <> -1)
- If (strTable Like "tb######*") And strTable <> "tb" & Format(Date, "yyyymm") Then
- If Len(CurrentDb.TableDefs(strTable).Connect) > 0 Then
- If (DLookup("[fldActive]", strTable) <> -1) Then
- dbDEL.TableDefs.Delete strTable 'delete table from the back end
- DoCmd.DeleteObject acTable, strTable 'Remove link to deleted tables from the front end
- End If
- End If
- End If
- Next
- dbDEL.Close
- Set dbDEL = Nothing
- Set tdfDEL = Nothing
- End Sub