Search code examples
sqlvbams-accessconstraintstable-relationships

MS Access: How to set outer join in constraint in VBA?


When I create a relationship between two tables manually at Database Tools > Relationships, Access lets me set the join properties so that it is either an inner join or a left or right outer join:

enter image description here

But when I create the relationship in VBA:

sSQL = "ALTER TABLE [" & sTable1 & "] ADD CONSTRAINT [" & sTable1 & "] " & _
                "FOREIGN KEY ([" & sKey & "]) REFERENCES [" & sTable2 & "]([" & sKey & "]);"
oDB.Execute sSQL

it creates the relationship as an inner join. I can see this by going to Database Tools > Relationships and looking at the relationship created by VBA:

enter image description here

I do not see a way to tell VBA what join properties to use, and it apparently decides on its own to make an inner join. (Another problem is that the little infinity symbol on the left side of the connector line means that Access thinks this is a one-to-many relationship, but since the linked fields are the primary key, it's one-to-one, as seen in the first image above.)

Microsoft has documentation on this process at:

There is also documentation on how to do this in DAO instead of SQL:

I don't see anything in any of that that allows me to choose the join properties.

I need the table relationship to be an outer join so that it includes all the records of one table and only the records of the second table in which the primary keys are equal. How can I create that relationship in VBA?


Solution

  • Updates:

    Looking at this more, you will need the attributes value of the relation object. Problem is, the attribute constants are bitwise and a bit elusive in the documentation. A value of 0 is the default you see and the rest have to be added up.

    So this is what you can do:

    Create a relationship in your database in the way you want it (left join, right join etc) and save. Assuming northwind is your database, in a function or module:

    Set dbsNorthwind = OpenDatabase("Northwind.mdb") 
     
     With dbsNorthwind 
     
     ' Display the attributes of the Northwind database's 
     ' relations. 
     Debug.Print "Attributes of relations in " & _ 
     .Name & ":" 
     For Each relLoop In .Relations 
     Debug.Print " " & relLoop.Name & " = " & _ 
     relLoop.Attributes 
     Next relLoop 
     .Close 
     End With
    

    The relation you created should show the attribute value you need. There is also a reference you can use here.

    Now when you go to create your own relation, you simply use the CreateRelation function (as in the documentation) and set the attributes you need like so:

    Set dbsNorthwind = OpenDatabase("Northwind.mdb") 
     
    With dbsNorthwind 
         Set relNew = .CreateRelation(Name, Table, ForeignTable, Attributes) 
         
         ' Add join fields
         relNew.Fields.Append relNew.CreateField("newFieldName") 
         relNew.Fields!newFieldName.ForeignName = "joinfieldName" 
         .Relations.Append relNew
         .Close 
    End With
    

    If for example you wanted a left join, the value you set for "Attributes" would be a combination of dbRelationLeft + another attribute constant represented as one numeric value.