[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Delete all tables in current project

Posted on 2004-08-02
7
Medium Priority
?
567 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
Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

656 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