Solved

Delete all tables in current project

Posted on 2004-08-02
7
516 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 11

Expert Comment

by:phileoca
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you all for your help!
0
 
LVL 1

Author Comment

by:weeb0
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
In the article entitled Working with Objects – Part 1 (http://www.experts-exchange.com/Microsoft/Development/MS_Access/A_4942-Working-with-Objects-Part-1.html), you learned the basics of working with objects, properties, methods, and events. In Work…
Familiarize people with the process of utilizing SQL Server views 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 Access…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.

772 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

12 Experts available now in Live!

Get 1:1 Help Now