Results 1 to 9 of 9
  1. #1
    gammaman is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2013
    Posts
    56

    Drop Table for each loop


    I am trying to drop multiple tables using a loop. I am getting a syntax error.

    Code:
    Private Sub cleanUp()
      Dim t As TableDef
      For Each t In CurrentDb.TableDefs
        If t.Name <> "VENDOR_TBL" Then
          DoCmd.RunSQL ("DROP TABLE" & t.Name & ";")
        End If
        
      Next
      MsgBox ("DONE")
    End Sub

  2. #2
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    You need a space after "DROP TABLE", otherwise the table name runs into the word TABLE, i.e.
    Code:
          DoCmd.RunSQL ("DROP TABLE " & t.Name & ";")

  3. #3
    gammaman is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2013
    Posts
    56
    Thank You. Missed that. However, I am still getting an error. Could it be because access is attempting to drop a tmp table? See attached.
    Attached Thumbnails Attached Thumbnails drop table.JPG  

  4. #4
    orange's Avatar
    orange is online now Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    Do you know which tables you want to drop?
    What about those beginning MSys*??

  5. #5
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    Possibly. It might not like the "~" symbol in the name.
    Do all the tables you want to delete have anything in common, i.e. a prefix or something, that you can limit your delete statements to ones meeting those conditions?

    Here is something that I have that may be helpful. I have some automated processes that import files, and sometimes there are import errors that I do not care about. So in order to keep my database from filling up with a bunch of Import Error tables, I run this code to keep it lean:
    Code:
    '   Delete any old import errors tables
        For Each tblDef In CurrentDb.TableDefs
            If InStr(1, tblDef.Name, "ImportErrors") > 0 Then
                DoCmd.SelectObject acTable, tblDef.Name, True
                DoCmd.DeleteObject acTable, tblDef.Name
            End If
        Next tblDef
    This at least shows you some other syntax for deleting tables.

  6. #6
    gammaman is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2013
    Posts
    56
    Want to drop all user defined tables except for what is specified in my code. So all access system and tmp tables should not be deleted. Is there a better way?

    Code:
    Private Sub cleanUp()
      Dim t As TableDef
      For Each t In CurrentDb.TableDefs
        If t.Name <> "VENDOR_TBL" And t.Name Like "~*" = False And t.Name Like "MSy*" = False Then
          MsgBox "DROP TABLE " & t.Name & ";"
          DoCmd.RunSQL ("DROP TABLE " & t.Name)
        End If
        
      Next
      MsgBox ("DONE")
    End Sub

  7. #7
    HiTechCoach's Avatar
    HiTechCoach is offline MS MVP - Access Expert
    Windows 8 Access 2013
    Join Date
    Jul 2010
    Location
    Oklahoma, USA
    Posts
    702
    How are you handling the case where 'user defined tables have relationships?

    Assuming this is a split database. It may be easier to export the tables you do want to keep to another database and rename the back I would create a new separate front end that only does this task.

    If yu do go with thw delete method, I highly recommend that you run a compact a bwefeore the data gets used .

  8. #8
    gammaman is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2013
    Posts
    56
    I am doing both actually. Before deleting I am exporting them. However, I am still getting an error in the drop statement.

    Code:
    Private Sub cleanUp()
      Dim t As TableDef
      Dim SQL As String
      
      
      
     
      For Each t In CurrentDb.TableDefs
      SQL = "DROP TABLE " & t.Name
       'MsgBox SQL
        If t.Name <> "VENDOR_TBL" And t.Name Like "~*" = False And t.Name Like "MSy*" = False And t.Name Like "*$*" = False Then
          MsgBox SQL
        db.Execute SQL
        End If
        
      Next
      MsgBox ("DONE")
    End Sub

  9. #9
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    Have you tried the code that I provided, that deletes the tables using a different command?

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 12
    Last Post: 06-05-2015, 04:27 PM
  2. Replies: 9
    Last Post: 05-08-2015, 02:36 PM
  3. Drop/Create table in loop
    By gammaman in forum Programming
    Replies: 1
    Last Post: 10-09-2013, 05:34 PM
  4. Replies: 3
    Last Post: 03-10-2013, 07:04 AM
  5. Replies: 3
    Last Post: 10-19-2011, 01:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums