Search code examples
ms-accesssubform

Multiple Disjunctive Conditions for MS Access Subforms


In configuring a MS Access subform, you can link master fields in the parent form to child fields in the child form. You can also have multiple links, but multiple links are conjunctive, e.g. having 2 links results in only rows that satisfy both link 1 and link 2.

I'm trying to have multiple disjunctive links (link 1 or link 2).

To provide more context, I have a table that expresses relationships between two objects so it includes two foreign keys referring to the same table. In the subform, I want to include rows in which the object's ID matches either foreign keys.


Solution

  • As others have mentioned, it is not possible to use MS Access' very basic linking system to do anything more than inner joins. However, I managed to figure out a simple workaround: using the parent form's Current event to update the subform's RecordSource.

    This solution is a general one that can be used for subforms that have a more complex relationship with the parent form:

    Private Sub Form_Current()
        Dim sql as String
    
        'This is the SQL statement for my situation, but you can write anything.
        sql = "SELECT * FROM SourceTable WHERE fk1 = " & Me![ID] & " OR fk2 = " & Me![ID]
        SubformName.Form.RecordSource = sql
    End Sub