Results 1 to 15 of 15
  1. #1
    d9pierce1 is offline Expert
    Windows 10 Access 2019
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    776

    ComboBox with Where clause question

    Hi all,


    Could I please get some assistance in writing two SELECT Statement on two Combo Boxes?

    What I would like is to:
    On cboFilterRole I would like to make the select statement so that If the cboFilterIndustry Is NotNull,
    then BusinessPrimaryRoleID, tblBusinessPrimaryRole, BusinessPrimaryIndustryID FROM tblBusinessPrimaryRole
    WHERE BusinessPrimaryIndustryID= cboFilterIndustry and if it is null, then BusinessPrimaryRoleID, tblBusinessPrimaryRole, BusinessPrimaryIndustryID FROM tblBusinessPrimaryRole

    On cboFilterFunction I would like to make the select statement so that If the cboFilterIndustry Is NotNull OR cboFilterRole Is NotNull ,
    then BusinessPrimaryRoleID, tblBusinessPrimaryRole, BusinessPrimaryIndustryID FROM tblBusinessPrimaryRole
    WHERE BusinessPrimaryIndustryID= cboFilterIndustry OR BusinessPrimaryRoleID= On cboFilterRole and if it is null, then BusinessPrimaryFunctionID, BusinessPrimaryFunction, BusinessPrimaryRoleID, BusinessPrimaryIndustryID FROM tblBusinessPrimaryFunction

    This way if I select anything from the second and third combos it only shows me results that match the previous combo
    Here is the table, row source info currently:
    Code:
    
    'tblBusinessPrimaryIndustry
    'tbl Fields:
        BusinessPrimaryIndustryID
        BusinessPrimaryIndustry
    'ComboBox:
        cboFilterIndustry
    'Row Source:
        SELECT BusinessPrimaryIndustryID, BusinessPrimaryIndustry FROM tblBusinessPrimaryIndustry ORDER BY BusinessPrimaryIndustry; 
    
    
    'tblBusinessPrimaryRole
    'tbl Fields:
        BusinessPrimaryRoleID
        tblBusinessPrimaryRole
        BusinessPrimaryIndustryID
    'ComboBox:
        cboFilterRole
    'Row Source:
        SELECT BusinessPrimaryRoleID, tblBusinessPrimaryRole, BusinessPrimaryIndustryID FROM tblBusinessPrimaryRole ORDER BY BusinessPrimaryRole; 
    
    
    'tblBusinessPrimaryFunction
    'tbl Fields:
        BusinessPrimaryFunctionID
        BusinessPrimaryFunction
        BusinessPrimaryRoleID
        BusinessPrimaryIndustryID
    'ComboBox:
        CboFilterFunction
    'Row Source:
        SELECT BusinessPrimaryFunctionID, BusinessPrimaryFunction, BusinessPrimaryRoleID, BusinessPrimaryIndustryID  FROM tblBusinessPrimaryFunction ORDER BY BusinessPrimaryFunction;
    Thank you all!
    Dave

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    d9pierce1 is offline Expert
    Windows 10 Access 2019
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    776
    Thanks pbaldy,
    I pretty much have the cascading combos down but what I dont have is how if I dont select from the first, i still want to be able to select from second or third. But if I do select from the first then I want to filter down the list on the second and so on.

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Did you try anything? The requery method would probably work with the tweak in the query criteria described in the first link. You could also test the first in code and set the second's row source either way depending on whether a selection had been made.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    d9pierce1 is offline Expert
    Windows 10 Access 2019
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    776
    I have tried all sorts of things and couldnt get it to work. I can cascade it but I cant get it not to cascade?
    I am stumped...

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    We can't fix what you don't post.

    The query criteria tweak should make it pull all records if nothing is selected in the combo.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    d9pierce1 is offline Expert
    Windows 10 Access 2019
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    776
    I guess I dont understand the query tweek as this is a row source on a combo box and does not have a query it is pulling info from?
    I am not real good at these things so I may not just understand it. I thought there was a way to like nest the Select statement?
    Sorry

  8. #8
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    The row source is itself a query, just not a saved one you can see in the navigation pane. You can click on the ellipsis and put in design view like any other. Try

    SELECT BusinessPrimaryRoleID, tblBusinessPrimaryRole, BusinessPrimaryIndustryID
    FROM tblBusinessPrimaryRole
    WHERE BusinessPrimaryIndustryID = cboFilterIndustry OR cboFilterIndustry Is Null
    ORDER BY BusinessPrimaryRole;
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  9. #9
    davegri's Avatar
    davegri is online now Excess Access
    Windows 11 Access 2019
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,413
    See this for an approach. If any combobox changes, the rowsource for all the others is updated as well as the recordsource for the form.

    Ultimate Dynamic Multiple Cascading Comboboxes, synchronized in any order (accessforums.net)

  10. #10
    d9pierce1 is offline Expert
    Windows 10 Access 2019
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    776
    Hi pbaldy
    OK, I got some of it working now and thank you for this!
    I am having an issues with the 3rd combo (CboFilterFunction)
    Below is the code I am using on this but there is something wrong with the code as it just gives all results. doesnt create an error though?
    But I am close I believe!

    Code:
    'cboFilterRole: RowSource and works Great
    SELECT BusinessPrimaryRoleID, BusinessPrimaryRole, BusinessPrimaryIndustryID FROM tblBusinessPrimaryRole WHERE BusinessPrimaryIndustryID = cboFilterIndustry OR cboFilterIndustry Is Null;
     
    'CboFilterFunction: RowSource and does not work?
    'This is just not the correct method but not sure how to write it?
    SELECT BusinessPrimaryFunctionID, BusinessPrimaryFunction, BusinessPrimaryIndustryID, BusinessPrimaryRoleID FROM tblBusinessPrimaryFunction WHERE BusinessPrimaryFunctionID AND BusinessPrimaryRoleID = cboFilterIndustry OR cboFilterIndustry Is Null AND cboFilterRole OR cboFilterRole Is Null;
    
    
    
    
    Private Sub cboFilterIndustry_AfterUpdate()
        Call bListBoxFilter
        Me.cboFilterRole.Requery
        Me.CboFilterFunction.Requery
    End Sub
    Private Sub cboFilterRole_AfterUpdate()
        Call bListBoxFilter
        Me.CboFilterFunction.Requery
    End Sub
    Thank you
    Dave

  11. #11
    d9pierce1 is offline Expert
    Windows 10 Access 2019
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    776
    Hi davegri
    Thank you for this and will continue reviewing it as it was pretty intimidating for me! I will continue to try to learn this for future reference
    Thanks again
    Dave

  12. #12
    davegri's Avatar
    davegri is online now Excess Access
    Windows 11 Access 2019
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,413
    Quote Originally Posted by d9pierce1 View Post
    Hi davegri
    Thank you for this and will continue reviewing it as it was pretty intimidating for me! I will continue to try to learn this for future reference
    Thanks again
    Dave
    We're always learning. I encounter plenty of coding myself that is intimidating!

  13. #13
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Quote Originally Posted by d9pierce1 View Post
    Hi pbaldy
    OK, I got some of it working now and thank you for this!
    I am having an issues with the 3rd combo (CboFilterFunction)
    Below is the code I am using on this but there is something wrong with the code as it just gives all results. doesnt create an error though?
    But I am close I believe!

    Code:
    'cboFilterRole: RowSource and works Great
    SELECT BusinessPrimaryRoleID, BusinessPrimaryRole, BusinessPrimaryIndustryID FROM tblBusinessPrimaryRole WHERE BusinessPrimaryIndustryID = cboFilterIndustry OR cboFilterIndustry Is Null;
     
    'CboFilterFunction: RowSource and does not work?
    'This is just not the correct method but not sure how to write it?
    SELECT BusinessPrimaryFunctionID, BusinessPrimaryFunction, BusinessPrimaryIndustryID, BusinessPrimaryRoleID FROM tblBusinessPrimaryFunction WHERE BusinessPrimaryFunctionID AND BusinessPrimaryRoleID = cboFilterIndustry OR cboFilterIndustry Is Null AND cboFilterRole OR cboFilterRole Is Null;
    
    
    
    
    Private Sub cboFilterIndustry_AfterUpdate()
        Call bListBoxFilter
        Me.cboFilterRole.Requery
        Me.CboFilterFunction.Requery
    End Sub
    Private Sub cboFilterRole_AfterUpdate()
        Call bListBoxFilter
        Me.CboFilterFunction.Requery
    End Sub
    Thank you
    Dave
    When you mix And with Or you have to use parentheses to clarify the logic. You've also got a few stray items in there. Try

    SELECT BusinessPrimaryFunctionID, BusinessPrimaryFunction, BusinessPrimaryIndustryID, BusinessPrimaryRoleID
    FROM tblBusinessPrimaryFunction
    WHERE (BusinessPrimaryIndustryID = cboFilterIndustry OR cboFilterIndustry Is Null)
    AND (BusinessPrimaryRoleID = cboFilterRole OR cboFilterRole Is Null)
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  14. #14
    d9pierce1 is offline Expert
    Windows 10 Access 2019
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    776
    Hi pbaldy
    Thank you so much! I dont know how you keep all these syntaxes, brackets and parentheses straight.
    But I sure am glad you can!
    Again, Thank you for all your help, it worked perfectly!
    Dave
    Marked Solved

  15. #15
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Quote Originally Posted by d9pierce1 View Post
    Hi pbaldy
    Thank you so much! I dont know how you keep all these syntaxes, brackets and parentheses straight.
    But I sure am glad you can!
    Again, Thank you for all your help, it worked perfectly!
    Dave
    Marked Solved
    Happy to help Dave! Once you've made the same mistakes time after time like I have, it slowly starts to sink in.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. What instead where clause? Interview question
    By jaryszek in forum Access
    Replies: 4
    Last Post: 11-12-2017, 02:01 AM
  2. Question on an If clause
    By Ayiramala in forum Access
    Replies: 8
    Last Post: 11-27-2015, 06:30 PM
  3. combobox question
    By billcar2006 in forum Access
    Replies: 1
    Last Post: 03-05-2015, 07:34 AM
  4. Combobox question
    By manic in forum Forms
    Replies: 3
    Last Post: 02-05-2012, 04:55 PM
  5. Combobox Question
    By Trojnfn in forum Access
    Replies: 2
    Last Post: 10-12-2011, 11:28 AM

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