I need to delete relationships using VBA so that I can delete the linked tables, then link different tables and re-create the relationship.
This is the code I use to create the relationship

Here is the relationship window, and there are 2 copies of TblJoinComputer BranchTblComput ers
It would appear that if the tables are local, I get line 4 as expected. using the code
Assuming my FE is on Computer 1, TblComputers on Computer2 and TblJoinComputer Branch on Computer3 ... Don't ask
it would appear Access creates it's own name for the relationship as on line 1 (Difficult to see) but it is
{C40F1BCB-C7D2-4ED0-8DFF-EADBF5B7E583}
This code works to delete the relationship created with my name
I appreciate the picture of the table is not very clear but the only difference between line 1 and line 4 is in the last column where on line 1 it is:
and on Line 4 it is:
My problem is deleting the
{C40F1BCB-C7D2-4ED0-8DFF-EADBF5B7E583} relationship.
How do I find the name or do I have to trawl through the MSysRelationshi ps table to match the names of the 4 fields that I do know and delete them.
Sorry this post is so long winded.
Phil
This is the code I use to create the relationship
Code:
Sub CreateRelation(RelName As String, TabPrime As String, TabForeign As String, FldPrime As String, FldForeign As String) Dim MyDb As DAO.Database Dim Rel As DAO.Relation Dim Fld As DAO.Field On Error GoTo CreateRelationDAO_Err 'Initialize Set MyDb = CurrentDb() ' Check if relationship already there For Each Rel In MyDb.Relations If Rel.Name = RelName Then Exit Sub End If Next Rel 'Create a new relation. Set Rel = MyDb.CreateRelation(RelName) 'Define its properties. With Rel 'Specify the primary table. .Table = TabPrime 'Specify the related table. .ForeignTable = TabForeign 'Specify attributes for cascading updates and deletes. .Attributes = 3 'Add the fields to the relation. 'Field name in primary table. Set Fld = .CreateField(FldPrime) 'Field name in related table. Fld.ForeignName = FldForeign 'Append the field .Fields.Append Fld 'Repeat for other fields if a multi-field relation. End With 'Save the newly defined relation to the Relations collection. MyDb.Relations.Append Rel CreateRelationDAO_Exit: 'Clean up Set Fld = Nothing Set Rel = Nothing Set MyDb = Nothing 'Debug.Print "Relation created." Exit Sub CreateRelationDAO_Err: el = Erl en = Err.Number ed = Err.Description ctrlfnctnm = "GetDescription" Call Form_Err(en, ed, ctrlfnctnm, el, errMsgStr) Resume CreateRelationDAO_Exit End Sub
Here is the relationship window, and there are 2 copies of TblJoinComputer BranchTblComput ers
It would appear that if the tables are local, I get line 4 as expected. using the code
Code:
CreateRelation "TblJoinComputerBranchTblComputers", "TblJoinComputerBranch", _ "TblComputers", "ComputerID", "ComputerID"
it would appear Access creates it's own name for the relationship as on line 1 (Difficult to see) but it is
{C40F1BCB-C7D2-4ED0-8DFF-EADBF5B7E583}
This code works to delete the relationship created with my name
Code:
Function DeleteRelationship(RelName As String) ' DeleteRelationship ("TblJoinComputerBranchTblComputers") On Error GoTo DeleteRelationship_Err Dim MyDb As DAO.Database Set MyDb = CurrentDb MyDb.Relations.Delete RelName Set MyDb = Nothing DeleteRelationship_Exit: Exit Function DeleteRelationship_Err: If Err = 3265 Then ' Relationship doesn't exist Resume DeleteRelationship_Exit Else el = Erl en = Err.Number ed = Err.Description ctrlfnctnm = "RElationship" Call Form_Err(en, ed, ctrlfnctnm, el, errMsgStr) Resume DeleteRelationship_Exit End If End Function
Code:
{C40F1BCB-C7D2-4ED0-8DFF-EADBF5B7E583}
Code:
TblJoinComputerBranchTblComputers
{C40F1BCB-C7D2-4ED0-8DFF-EADBF5B7E583} relationship.
How do I find the name or do I have to trawl through the MSysRelationshi ps table to match the names of the 4 fields that I do know and delete them.
Sorry this post is so long winded.
Phil
Comment