Deleting all empty Tables

dubs

New Member
Joined
Jul 10, 2003
Messages
45
Hi all,

ive imported a database into access to work with but find that out of the 450 tables only 150 or so are being used, is there some code that would find and delete tables with no contents?

greg
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Hi Greg,

Following script might help. You can use it in VBA (in any office application) or as vbscript file (by saving as filename.vbs by using Notepad and double click on it to run). Remember to change database file path in code (dbpath).

Code:
Call DeleteEmptyTables

Sub DeleteEmptyTables()
Dim DAO
Dim dbmain
Dim rcset
Dim tbldef
Dim dbpath
dim i, j
dim tblArr()

 '******* Database file path - Change this as you need ********
  dbpath = "C:\MyFolder\mydbname.mdb"
 '************************************************************

  On Error Resume Next
  
  'Attempt to create DAO object
  Set DAO = CreateObject("DAO.DBEngine.36")
  If Err Then
    Err.Clear
    'Version might be 3.5 instead
    Set DAO = CreateObject("DAO.DBEngine.35")
  End If
  If Err Then
    'No way, DAO is not supported in this PC.
    'Suggestion might be using ADO instead.
    MsgBox "Cannot use DAO in this computer."
    Exit Sub
  End If
  
  'Create DAO database object
  Set dbmain = DAO.OpenDatabase(dbpath)

  If Err Then
  	Msgbox Err.Number & "-" & Err.Description
	Exit Sub
  End if
  
  i=0
  On Error Goto 0

  'Loop in tables
  For j = 0 to dbmain.TableDefs.Count - 1
    Set tbldef = dbmain.TableDefs(j)
    'Make sure if it is not a system table
    If tbldef.Attributes = 0 Then
      'Make sure recordcount is zero
      If tbldef.RecordCount = 0 Then
            'Store Table name
            Redim Preserve tblArr(i)
            tblArr(i)=tbldef.Name
            i=i+1
      End If
    End If
  Next 

  'Delete empty tables
  For j = 0 to i-1
   dbmain.TableDefs.Delete tblArr(j)
  Next

  'Close database
  dbmain.Close
  If i > 0 then
	Msgbox i & " empty table(s) have been deleted."
  Else
	Msgbox "There is no empty tables."
  End if
End Sub

It uses DAO and alternative code could be created with ADO as well. Please try it. And please take a backup of your database file. Script is supposed to delete the empty tables which is not system ones but .. in case something wrong make sure you got a backup :).

I hope it helps.
Suat
 
Upvote 0

Forum statistics

Threads
1,221,844
Messages
6,162,346
Members
451,760
Latest member
samue Thon Ajaladin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top