Solved

Delete all tables in current project

Posted on 2004-08-02
7
525 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Today's users almost expect this to happen in all search boxes. After all, if their favourite search engine juggles with tens of thousand keywords while they type, and suggests matching phrases on the fly, why shouldn't they expect the same from you…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

910 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

23 Experts available now in Live!

Get 1:1 Help Now