Results 1 to 6 of 6
  1. #1
    Sam_NY is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2015
    Posts
    17

    Filtering form by joined look-up field in a subform

    Can't quite wrap my head around this. I'd like to filter a form based on a field in the subform that is itself a lookup field from a third table. I was trying to use the combo box / AfterUpdate method described here: http://allenbrowne.com/ser-28.html

    Which suggests the following SQL in the After Update field for the combo box:

    Code:
    Dim strSQL As String
    If IsNull(Me.cboShowSup) Then
        ' If the combo is Null, use the whole table as the RecordSource.
        Me.RecordSource = "tblProduct"
    Else
        strSQL = "SELECT DISTINCTROW tblProduct.* FROM tblProduct " & _
            "INNER JOIN tblProductSupplier ON " & _
            "tblProduct.ProductID = tblProductSupplier.ProductID " & _
            "WHERE tblProductSupplier.SupplierID = " & Me.cboShowSup & ";"
        Me.RecordSource = strSQL
    End If
    But I'm not sure how to write in the other join. So, using this example, it would be like if tblProductSupplier.SupplierID was joined to a third table--let's say tblOtherTable.SupplierIDs



    Also the above example specifies tblProductSupplier as Row Source in Properties for the Combo Box, but I think I should put tblOtherTable for my situation (yes?).

    The result I'm looking for is when I enter or choose the SupplierID from the combo box, it filters the records in the main form to show only those with subform records matching that ID. Hopefully I'm asking this clearly but let me know if not. Thanks.

  2. #2
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,114
    I suggest you build a query using your three tables that gives you the right records (look up query by form to see how to build one that uses the subform controls as parameters). Once you have it return the right records save it and use that in the Else part of the code you posted,
    Code:
    If IsNull(Me.cboShowSup) Then
        ' If the combo is Null, use the whole table as the RecordSource.
        Me.RecordSource = "tblProduct"
    Else
        'strSQL = "SELECT DISTINCTROW tblProduct.* FROM tblProduct " & _
            "INNER JOIN tblProductSupplier ON " & _
            "tblProduct.ProductID = tblProductSupplier.ProductID " & _
            "WHERE tblProductSupplier.SupplierID = " & Me.cboShowSup & ";"
        Me.RecordSource = "qryMyNewQueryBySupplierID"
    End If
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  3. #3
    Join Date
    Apr 2017
    Posts
    1,679
    This feels backward and awkward! Much more simple is to have main form (bound or unbound) where you select supplier, and a continuous subform with form based on tblProductSupplier as source. You link subform with main form through supplier ID in both tables (bound main form) or through supplier combo box in main form, and supplier ID in tblProductSupplier (unbound main form).

    As result:
    Subform displays only and all products registered as supply by supplier active in main form;
    Any product added into subform will be automatically linked with supplier active in main form (NB! This means you have to hide the control linked to supplier ID in subform to disable any possibility for users to mess up with it. At same time, the control must exist. as supplier ID is entered into control, but is stored in table only, when the record is saved. Btw, this allows user to cancel the new unsaved entry into subform at will pressing Esc - without creating any orphan entries!).

    With this solution there is no need for VBA.

  4. #4
    Sam_NY is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2015
    Posts
    17
    Thanks, Vlad. I'm giving this a shot. I do already have a query that returns the right records, but was having trouble figuring out how to put that into the Else statement. Will revisit though!

  5. #5
    Sam_NY is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2015
    Posts
    17
    Thanks ArvilLannemets. I get what you're saying and will likely create some version of what you're saying (it does make a lot of sense). And I know it seems backward, but I guess the point of the filter I'm trying to create is for when a user is navigating the main form and decides they want to see, in that form, only the products that have a certain supplier. For the purpose of my post, I used the example from the site I linked to, but in my actual use it makes sense for the users' workflows. I guess I could open the main form to the relevant record from the new form you suggest (which...also new to me!).

  6. #6
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,114
    Hi Sam,

    If the combo is on the subform I think you will need to change the code to Me.Parent.Form.RecordSource = "qryMyNewQueryBySupplierID". And be aware that changing the recordsource will do an automatic requery so the main form will jump to the first record returned by the new query.

    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 1
    Last Post: 05-06-2015, 04:28 AM
  2. Filtering Data in a Subform for a form
    By mchadwick in forum Forms
    Replies: 1
    Last Post: 01-02-2015, 01:54 PM
  3. Form filtering and adding records to subform
    By Albathien in forum Forms
    Replies: 2
    Last Post: 05-09-2014, 09:31 AM
  4. Replies: 1
    Last Post: 10-10-2012, 03:33 PM
  5. Replies: 3
    Last Post: 12-28-2011, 02:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums