Solved

Delete all tables in current project

Posted on 2004-08-02
7
537 Views
Last Modified: 2012-06-27
Hi,

   I want to delete all tables in the current database from a module in access 97/2000.
There is about 120 tables so I want to know the name of each table and using SQL query I want to delete'em.

I tried to do the work, but it seem the module (ADOX) need a read only access to de database,

Thank you for helping me, since I'm new in access programming and I don't know how I can achieve this.

Kris
0
Comment
Question by:weeb0
7 Comments
 
LVL 11

Expert Comment

by:phileoca
ID: 11694712
greetings.

save this in a module and call it:

Option Compare Database
Option Explicit
Function Resetdatabase()
   
    Dim DB As Database
    Dim T As TableDef
    Dim TType As String
    Dim TName As String
    Dim Attrib As String
    Dim I As Integer
    Dim tbl, fld
    Dim locFldType
    Dim locTblName
    Dim locTblType
       
    Dim locQuery As String
       
    Set DB = CurrentDb()
    'from the microsoft knowledgebase:
    'http://support.microsoft.com/default.aspx?scid=kb;en-us;117536&Product=acc97
    For I = 0 To DB.TableDefs.Count - 1
        Set T = DB.TableDefs(I)
        TName = T.Name
        Attrib = (T.Attributes And dbSystemObject)
        locTblType = DLookup("Type", "MSysObjects", "[Name]='" & TName & "'")
           
            'if not a System or Linked Table then delete data
            If (Attrib = 0) And (locTblType <> 6) Then
                'delete data here
                 locQuery = "DELETE * FROM " & TName
                 DoCmd.RunSQL locQuery
             End If
        'MsgBox TName & IIf(Attrib, ": System Table", ": Not System Table")
    Next
        MsgBox "All Tables Updated"
End Function
0
 
LVL 11

Expert Comment

by:phileoca
ID: 11694724
Note: this is set up so that it WILL NOT delete data inside Linked Tables or System Tables.

But now that i'm reading your question again, it appears you want to delete all the tables, not the data inside the tables.
Is this correct?
0
 
LVL 5

Expert Comment

by:Jag5x5
ID: 11694759
you can do this with a for each loop on the alltables collection in currentproject.

dim t as table

for each t in currentproject.alltables
     if left(t.name,4) = "msys" then
     else
          t.delete
     end if
next t

That is not exact but it uses DAO.  I have the exact code but don't know where it is.
0
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
LVL 11

Expert Comment

by:phileoca
ID: 11694808
okay, i modified the code to delete the Table Instead.
If you just want to delete the DATA, use the above Code.

If you want to delete all the tables (excluding System and Linked Tables) use the following code:


Option Compare Database
Option Explicit
Function Resetdatabase()
   
    Dim DB As Database
    Dim T As TableDef
    Dim TType As String
    Dim TName As String
    Dim Attrib As String
    Dim I As Integer
    Dim tbl, fld
    Dim locFldType
    Dim locTblName
    Dim locTblType
       
    Dim locQuery As String
       
    Set DB = CurrentDb()
    'from the microsoft knowledgebase:
    'http://support.microsoft.com/default.aspx?scid=kb;en-us;117536&Product=acc97
    For I = 0 To DB.TableDefs.Count - 1
        Set T = DB.TableDefs(I)
        TName = T.Name
        Attrib = (T.Attributes And dbSystemObject)
        locTblType = DLookup("Type", "MSysObjects", "[Name]='" & TName & "'")
           
            'if not a System or Linked Table then delete data
            If (Attrib = 0) And (locTblType <> 6) Then
                'delete data here
                 DoCmd.DeleteObject acTable, TName
             End If
        'MsgBox TName & IIf(Attrib, ": System Table", ": Not System Table")
    Next
        MsgBox "All Tables Updated"
End Function
0
 
LVL 44

Accepted Solution

by:
GRayL earned 50 total points
ID: 11694834
Once you run this your tables are gone forever!  Better have a backup handy.

Create a Subroutine

Private Sub RemoveTables()

dim mydb as database, mytds as tabledefs, mytd as tabledef
set mydb = currentdb
set mytds = mydb.tabledefs
for each mytd in mytds
mytds.delete mytd.name

End Sub

call this by entering:  RemoveTables  - in the immediate pane of the VB Editor
0
 
LVL 1

Author Comment

by:weeb0
ID: 11695423
Thank you all for your help!
0
 
LVL 1

Author Comment

by:weeb0
ID: 11695552
I found an other solution to this, it shows up the hidden informations behin access:

Option Compare Database
Option Explicit

Sub delete_all_tables() 'macro pour effacer toutes les tables (non systemes) de la database

    Dim db As Database
    Dim Lrs As DAO.Recordset
    Dim LSQL As String
    Dim LGST As String
    Dim x As Integer

    'Open connection to current Access database
    Set db = CurrentDb()

    'Create SQL statement to retrieve value from GST table
    LSQL = "select Name from MSysObjects where Type = 1 and not name like 'MSys*'"

    Set Lrs = db.OpenRecordset(LSQL)

   'Retrieve value if data is found
    If Lrs.EOF = False Then
       
        'do while every tables are deleted
        For x = 0 To Lrs.RecordCount Step 1
       
        LGST = Lrs("Name")
       
        ExecuteSQLDDL ("drop table " + LGST)
        Lrs.MoveNext
        Next x
    Else 'no table to delete
        LGST = "Not found"
        MsgBox ("AUCUNE TABLE A EFFACER!")
    End If

    Lrs.Close
    Set Lrs = Nothing
   

    'GetGST = LGST

End Sub

' fonction pour faire des requetes en DDL (data definition language)

Sub ExecuteSQLDDL(SQLString As String)
   Dim db As DAO.Database, qd As DAO.QueryDef
   Set db = DBEngine.Workspaces(0).Databases(0)
   Set qd = db.CreateQueryDef("")
   qd.SQL = SQLString
   qd.Execute
   db.Close
End Sub
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now