?
Solved

Delete all tables in current project

Posted on 2004-08-02
7
Medium Priority
?
561 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
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 200 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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

777 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