Sub DeleteTableTest3(pMyTable As String)
' This procedure deletes specified tables and any
' existing relationships the tables are participating
' in.
' Used inappropriately, it will have a devastating
' effect on an application.
' For safety's sake, I've commented-out the actual
' commands (they follow debug.print in every case).
' Once you've examined the code and are ready to go,
' remove the comments (and you may want to
' comment-out the debug.print commands).
Dim db As Database
Dim td As TableDef
Dim Test As String
Dim tName As String
Dim thisrel As Relation
Set db = CurrentDb
On Error Resume Next
tName = pMyTable
'**********
'Does table tName exist?
'If true, delete it;
'**********
Test = db.TableDefs(tName).Name
If Err <> 3265 Then
'**********
' Since the delete action will fail if the
' table is participating in any relation, first
' find and delete existing relations for table.
'**********
For Each thisrel In db.Relations
If thisrel.Table = tName Or thisrel.ForeignTable = tName Then
Debug.Print tName & " | " & thisrel.Name
'db.Relations.Delete thisrel.Name
End If
Next thisrel
'**********
' Now, we're ready to delete the table.
'**********
Debug.Print tName & " will be deleted"
'docmd.SetWarnings False
'docmd.DeleteObject acTable, tName
'docmd.SetWarnings True
End If
db.Close
Set db = Nothing
End Sub