Question : How to delete all relationships between tables before deleting tables within Access using VBA?

All,

I've got the following coding but whether I use the SQL or just use DoCmd.DeleteObject I get the error - run-time error '2387'.  Anyway I can enhance below to include a function to check of relationships tied to a table first and then delete those relationships before deleting the table?   Obviously if available please provide the coding to do so.  

I was unable to find a function to delete all relationships in a database but if provided I could tweak it to do what I need..  And this is not all that I"m working on to narrow the searchi listing down....  Thank you for some coding replies.  If no time now, I can certainly wait.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
Option Compare Database
Option Explicit

Private Sub Delete_Tables()

  Call Delete_Table("PROJ_ME")
  Call Delete_Table("PROJ_EQ")
  Call Delete_Table("PROJ_RM")
  Call Delete_Table("PROJ_DPT")
  Call Delete_Table("ALTSORT")
  Call Delete_Table("PROJ_INF")
  
End Sub

Private Sub Form_Open(Cancel As Integer)

  Call Delete_Tables
  
End Sub

Private Sub Delete_Table(sTable As String)
  
'  Dim sSQL As String
'  sSQL = "DROP TABLE [" & sTable & "]"
'  CurrentDb.Execute sSQL
'  DoEvents

  DoCmd.DeleteObject acTable, sTable

End Sub

Answer : How to delete all relationships between tables before deleting tables within Access using VBA?

Private Sub Delete_Table(sTable As String)
    Dim rex As Relations
    Dim rel As Relation
    Set rex = CurrentDb.Relations
    Dim i As Long
    For i = rex.Count - 1 To 0 Step -1
        Set rel = rex(i)
        If rel.Table = sTable Then
            rex.Delete rel.Name
        End If
    Next i
  DoCmd.DeleteObject acTable, sTable
End Sub
Random Solutions  
 
programming4us programming4us