Results 1 to 5 of 5
  1. #1
    registoni is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    13

    Code to drop tables and optimize database on closure

    I am using lots of temp tables , and would like to drop them on the closure of database. Is there an elegant way of doing this automatically, and also to include option to compact and repair database? Note all my temp tables start with "temp_****".

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    You can either explicitly reference each table in separate deletion actions or cycle through tables collection. Or leave the tables and just delete records.

    Why do you have a lot of temp tables?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    registoni is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    13
    my numerous reports use a lot of queries (30/50 per each report and subreport) and without temp tables it was impossible to run the report or save it.
    Can you show it as a sample vba code that would select all tables starting with temp_ and drop them, and then compact and save the database before closing?

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Bing: Access vba table collection
    http://msdn.microsoft.com/en-us/libr...ffice.10).aspx
    http://msdn.microsoft.com/en-us/libr...ffice.11).aspx

    Bing: Access vba compact on close
    http://www.fmsinc.com/MicrosoftAcces...-on-close.html
    http://stackoverflow.com/questions/1...atabase-in-vba

    Maybe:
    Code:
        Dim obj As AccessObject, dbs As Object
        Set dbs = Application.CurrentData
        ' Search for open AccessObject objects in AllTables collection.
        For Each obj In dbs.AllTables
            If tblList.Name Like "temp*" Then
                CurrentDb.Execute "DROP TABLE [" & tblList.Name & "]"
            End If
        Next obj
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    You might think about moving the temp tables to a different BE (maybe named "TempBE").
    Link the temp tables to the FE. The main BE won't bloat (as much) and you can delete the records in the temp BE , compact and repair, whenever..... not need to delete the tables.

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

Similar Threads

  1. Drop Down Menu Linking To Tables
    By tristangemus in forum Forms
    Replies: 9
    Last Post: 06-21-2013, 08:44 AM
  2. Replies: 1
    Last Post: 11-16-2011, 11:56 PM
  3. Replies: 3
    Last Post: 10-19-2011, 01:05 PM
  4. Activating A Drop Down Box Via Code
    By orcinus in forum Programming
    Replies: 2
    Last Post: 08-11-2011, 03:04 PM
  5. Optimize a select query
    By accessnewb in forum Queries
    Replies: 15
    Last Post: 07-21-2011, 01:22 PM

Tags for this Thread

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