deleting tables with relationships

K

Katrina

I have the function below to delete all tables from
another database (file.mdb) this command will delete all
the tables without relationships, but I need it to delete
all the tables, including the ones with relationships.
How can I fix this?
Thanks in advance for your help.

Function deletea()
Dim f
Set fs = CreateObject("scripting.filesystemobject")
Set f = fs.getfile("C:\file.mdb")

f.Attributes = 0
Dim dbSource As DAO.Database
Dim tdf As Object
Dim strSourceDB As String

strSourceDB = f
Set dbSource = DBEngine.OpenDatabase(strSourceDB)

For Each tdf In dbSource.TableDefs
If Left(tdf.NAME, 4) <> "MSys" Then
dbSource.TableDefs.delete tdf.NAME
Else

End If
Next tdf

f.Attributes = 1
dbSource.Close
Set dbSource = Nothing
End Function
 
D

Dirk Goldgar

Katrina said:
I have the function below to delete all tables from
another database (file.mdb) this command will delete all
the tables without relationships, but I need it to delete
all the tables, including the ones with relationships.
How can I fix this?
Thanks in advance for your help.

Function deletea()
Dim f
Set fs = CreateObject("scripting.filesystemobject")
Set f = fs.getfile("C:\file.mdb")

f.Attributes = 0
Dim dbSource As DAO.Database
Dim tdf As Object
Dim strSourceDB As String

strSourceDB = f
Set dbSource = DBEngine.OpenDatabase(strSourceDB)

For Each tdf In dbSource.TableDefs
If Left(tdf.NAME, 4) <> "MSys" Then
dbSource.TableDefs.delete tdf.NAME
Else

End If
Next tdf

f.Attributes = 1
dbSource.Close
Set dbSource = Nothing
End Function

Since you're deleting the tables, I assume it's okay to delete the
relationships first. Something like this ought to do it:

Dim iRel As Integer

With dbSource.Relations
For iRel = (.Count - 1) To 0 Step -1
.Delete .Item(iRel).Name
Next iRel
End With

After that you should be able to delete all the tables without
obstruction.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top