Page 1 of 2 12 LastLast
Results 1 to 15 of 30
  1. #1
    Dachbo is offline Advanced Beginner
    Windows 7 32bit Access 2013
    Join Date
    May 2015
    Posts
    42

    combo box with multiple values for filtering by criteria

    Hello,



    I have a combo box with some values like "Field11", "Field12"... (see image) which are some column-names in this table.

    Click image for larger version. 

Name:	Acces_Forum_Multiple Criteria.PNG 
Views:	34 
Size:	5.4 KB 
ID:	21120

    I would like to get only the people with an "Y" in the cell when I select "FieldXY". If I select "Field17" in the combo box I only want to get the one person... "Field13" the 6 person with a "Y"

    I tried it with a simple Query but I noticed it's not a good option. I think it requires VBA, could anybody give me some advice how the code could look like?

    Thanks!

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,625
    Looks like non-normalized data structure and that is what is making this so difficult.

    Build a form based on the table or query and apply filter criteria to the form. Use the intrinsic search/filter tools from ribbon and right click menu or use VBA to build filter string. http://www.allenbrowne.com/ser-62.html
    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
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,549
    You cant use form boxes in a query if there's nothing in them..so..
    Test all controls for a possible filter then build the where clause.


    Code:
    
    if not isnull(cboState) then   sWhere = sWhere & " and [state]='" & cboState & "'"
    if not IsNull(txtName) then    sWhere = sWhere & " and [Name]='" & txtName & "'"
    if not IsNull(chkContact) then sWhere = sWhere & " and [Contact]=" & chkContact.value
    
    
        'remove 1st And
    sWhere= mid(sWhere,5)
    
    
      'just use the filter
    
    
    me.filter = sWhere
    me.filterOn = true

  4. #4
    Dachbo is offline Advanced Beginner
    Windows 7 32bit Access 2013
    Join Date
    May 2015
    Posts
    42
    Thank you both.

    I'm little confused though. How do I use the .filter and how do I tell Access to list the person with the "Y" in the specific column in a new list box?

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,625
    We are also confused. Your OP does not mention a listbox. The suggestion is to have form bound to the dataset and use code to build filter criteria and apply to the form Filter property.
    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.

  6. #6
    Dachbo is offline Advanced Beginner
    Windows 7 32bit Access 2013
    Join Date
    May 2015
    Posts
    42
    oh sorry, I forgot to mention this.

    I have a combobox to select a column ""Field_XY" and when I click the go button all the people with a "Y" in this column, should show up in the listbox.
    Click image for larger version. 

Name:	List- and Combobox.PNG 
Views:	27 
Size:	3.1 KB 
ID:	21134

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,625
    Code will have to set the listbox RowSource property, in VBA something like:

    Me.listboxname.RowSource = "SELECT ID, Lastname, Firstname FROM tablename WHERE " & Me.comboboxname & "=True;"

    That assumes the fields are yes/no type. If they are text type:

    Me.listboxname.RowSource = "SELECT ID, Lastname, Firstname FROM tablename WHERE " & Me.comboboxname & "='Y';"

    The GO button is not necessary. Can put the code in combobox AfterUpdate event.
    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.

  8. #8
    Dachbo is offline Advanced Beginner
    Windows 7 32bit Access 2013
    Join Date
    May 2015
    Posts
    42
    thanks, works just fine!

  9. #9
    Dachbo is offline Advanced Beginner
    Windows 7 32bit Access 2013
    Join Date
    May 2015
    Posts
    42
    how do I edit the code, to filter it even more by some check boxes

  10. #10
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,625
    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.

  11. #11
    Dachbo is offline Advanced Beginner
    Windows 7 32bit Access 2013
    Join Date
    May 2015
    Posts
    42

    Post

    Code:
     Me.lstbx_EMPLOYEES.RowSource = "SELECT EMPLOYEES_ID FROM tbl_EMPLOYEES WHERE " & Me.cmbx_EMPLOYEES & "='Y';"
        
        If Me.chck_OAP1 = -1 Then
            Me.lstbx_EMPLOYEES.RowSource = "SELCET EMPLOYEES_ID FROM tbl_EMPLOYEES WHERE ='OA_P1';"
        
        End If
    When I step through the code:
    - it displays all entries with a "Y" in the list box,
    then
    - the If function will clear the list box, when it's checked, but there are records with "OP_P1"

    I think after the WHERE is something missing, but I don't know what.

  12. #12
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,625
    Need the field name to apply filter parameter to.

    WHERE [some field name here] = some data here
    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.

  13. #13
    Dachbo is offline Advanced Beginner
    Windows 7 32bit Access 2013
    Join Date
    May 2015
    Posts
    42
    it still doesn't work

    I tried it with different field names and data names.

  14. #14
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,625
    "doesn't work" means what - error message, wrong results, nothing happens? Post revised code.
    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.

  15. #15
    Dachbo is offline Advanced Beginner
    Windows 7 32bit Access 2013
    Join Date
    May 2015
    Posts
    42
    nothing happens, it steps through the code but doesn't bring up any results in the list box

    Code:
     If Me.chck_OAP1 = -1 Then
                      Me.lstbx_EMPLOYEES.RowSource = "SELCET EMPLOYEES_ID FROM tbl_EMPLOYEES WHERE [DEPTS] ='OA_P1';"
    
          End If

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Filtering query based on multiple rows values
    By GeorgeB in forum Queries
    Replies: 6
    Last Post: 12-05-2013, 06:17 PM
  2. Replies: 1
    Last Post: 07-31-2013, 06:31 PM
  3. Replies: 5
    Last Post: 08-07-2012, 10:12 PM
  4. Replies: 1
    Last Post: 07-12-2012, 08:39 AM
  5. Replies: 5
    Last Post: 03-12-2012, 02:58 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