Results 1 to 5 of 5
  1. #1
    fhickler is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Nov 2018
    Posts
    34

    Use field in table to filter other table

    Let’s say I have 2 tables brought into a form as subforms. "Subform1" has an index field of unique numbers called "Id" and "Subform2" has a number field called "MatchNum". I want to program a button on the form that, when clicked, will filter out every record in "Subform1" where the value of "Id" matches some value in the "Subform2" field "MatchNum".

    Like this:

    Subform1
    Id Other data...
    01
    02
    03
    04
    05

    Subform2
    Id MatchNum Other data...
    01 02
    02 02
    03 03
    04 02
    05 03


    Clicking the button would filter Subform1 as:

    Subform1
    Id Other data...
    01
    04
    05




    I'm looking for suggestions as to what VBA code to use for the button. Thanks in advance.

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,643
    Usually subform Master/Child Links manage synchronization of related forms. Subform1 appears to be the 'master' (parent) data and Subform2 the 'child' (dependent data). Normally child does not dictate to master.

    Might be able to do something like:

    Subform1.Form.RecordSource = "SELECT * FROM table WHERE ID IN (" & subform2.Form.RecordSource & ")"
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    fhickler is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Nov 2018
    Posts
    34
    Thank you. I managed to make some progress based on your response. I have this code attached to the button:

    strSQL = "SELECT * FROM table1 WHERE table1.ID IN (" & subform2.Form.MatchNum & ")"
    Me![subform1].Form.RecordSource = strSQL

    What this does is filters subform1 to show the record whose ID matches the currently selected record's MatchNum value in subform2. What I want is for it to do is filter subform1 so that it shows records whose ID matches ANY MatchNum value in subform2 (or doesn't show them - either way - I imagine once I get one of them, getting the inverse will be trivial)

    I also successfully tried:

    strSQL = "SELECT * FROM table1 WHERE table1.ID IN (3,5)"
    Me![subform1].Form.RecordSource = strSQL

    So it seems to me that all I have to do is get the values of the field MatchNum for all records in subform2 as a list. How would I do that?

    EDIT:
    I just came across this:
    https://stackoverflow.com/questions/...mn-in-an-array
    which indicates that I'd have to do it by looping through all the records. I guess I was hoping there was simpler way to do it. Is there?
    EDIT2:
    Just realized the above link was referring to VBA for Excel. Not sure if it is the same for Access.

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,643
    That link is for Excel. Excel sheets don't have recordsets.

    My example referenced RecordSource property, not a field name. But did not test and example was incomplete.

    If subform RecordSource is a simple SQL statement SELECT * FROM table2; following code will build an SQL statement.

    Subform1.Form.RecordSource = "SELECT * FROM table1 WHERE NOT ID IN (SELECT MatchNum FROM (" & Replace(Me.subform2.Form.RecordSource, ";", "") & "));"

    If subform RecordSource is simply a table or query name:

    "SELECT * FROM table1 WHERE NOT ID IN (SELECT MatchNum FROM (SELECT * FROM " & Me.subform2.Form.RecordSource & "));"

    Nested subquery serves as list. Including subform filter criteria will get a little more complicated. How is subform filtered?

    If you want to provide db for analysis, follow instructions at bottom of my post.




    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    fhickler is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Nov 2018
    Posts
    34
    Wow that worked. Thank you. Now all I have to do is study this code so I actually understand it!
    EDIT:
    I guess I pretty much understand it. One part that threw me was the Replace - it seemed you were removing semicolons from the list, but I wasn't sure why. When I removed the Replace, it still seemed to work. What is the purpose of the Replace?

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

Similar Threads

  1. Replies: 10
    Last Post: 03-16-2018, 08:26 AM
  2. Replies: 2
    Last Post: 09-23-2017, 10:51 PM
  3. Replies: 3
    Last Post: 01-01-2015, 05:25 PM
  4. filter 1 table based on values in another table
    By markjkubicki in forum Programming
    Replies: 1
    Last Post: 12-02-2014, 10:50 PM
  5. Replies: 5
    Last Post: 12-08-2011, 10:52 AM

Tags for this Thread

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