Page 2 of 2 FirstFirst 12
Results 16 to 30 of 30
  1. #16
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929

    Try changing spelling of SELCET to SELECT.
    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.

  2. #17
    Dachbo is offline Advanced Beginner
    Windows 7 32bit Access 2013
    Join Date
    May 2015
    Posts
    42
    oh thanks a lot, pretty stupid mistake.

    But now, I first get all the Employees with the "Y" entry, after the codes steps through the chck_OAP1 it only shows the Employees with OA_P1.
    How do I get only the Employees with the "Y" AND OA_P1?

  3. #18
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Use as many criteria as you want in the WHERE clause.

    You said user selects the field from combobox. I already showed you how to reference combobox.

    "SELECT Employees_ID, Lastname, Firstname FROM tbl_EMPLOYEES WHERE " & Me.comboboxname & "='Y' AND [DEPTS] ='OA_P1';"
    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.

  4. #19
    Dachbo is offline Advanced Beginner
    Windows 7 32bit Access 2013
    Join Date
    May 2015
    Posts
    42
    how do I do this with the if clause? I have more than one check box. It could be randomly selected. Maybe only one check box is true or several check boxes could be selected as true.

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

    Post

    I found something and changed it a little bit.

    The code steps through the first If Me.chck_OA and jumps to the BuildFilter but skips the If Len(... statement and jumps back to the If Me.chck_OAP1 and steps to the end of the code and then a msg box pops up and wants me to enter the value for Me.Filter

    Whats wrong with the code, it is not filling the FilterDEPTS string

    Code:
        Private Sub btn_filter_Click()
        Dim FilterDEPTS As String
        FilterDEPTS = ""
        
        If Me.chck_OA = -1 Then BuildFilter FilterDEPTS, "OA"
        If Me.chck_OAP1 = -1 Then BuildFilter FilterDEPTS, "OA_P1"
        
        
        Me.FilterOn = Len(FilterDEPTS) > 0
        Me.Filter = FilterDEPTS
        
    
        Me.lstbx_EMPLOYEES.RowSource = "SELECT EMPLOYEES_ID FROM tbl_EMPLOYEES WHERE " & Me.cmbx_EMPLOYEES & "='Y' AND [DEPTS] = Me.Filter;"  
    
    End Sub
    
    Sub BuildFilter(ByRef FilterDEPTS As String, sAdd As String)
            If Len(FilterDEPTS) > 0 Then
                FilterDEPTS = FilterDEPTS & "AND"
                FilterDEPTS = FilterDEPTS & sAdd
            End If
                
    End Sub

  6. #21
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    FilterDEPTS is not a global declared variable. It is declared within a procedure so it only has a life within that procedure. Doesn't matter that you use the same variable name in two procedures. They have no connection, therefore every time each procedure is run, its variable starts out empty. If you want a variable to be available to multiple procedures, it must be declared in the module header.

    Suggest you go back to the Allen Browne example and get that working before attempting to get more creative with code. For every control you want to contribute to the criteria string, build a conditional If Then.
    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.

  7. #22
    Dachbo is offline Advanced Beginner
    Windows 7 32bit Access 2013
    Join Date
    May 2015
    Posts
    42
    I got the basic idea from Allen Browne.

    How does it work to fill the strWhere string to look after multiple values for only one column? In the example he uses one value in the string for one column. I would like to use multiple values for only one column.

    Something like this:
    If OA, OA_P1, OA_P2... are checked, look in column DEPTS after OA or OA_P1 or OA_P2 and give me all records with either OA, OA_P1 and OA_P2....

    How do I fill the strWhere string for this and how does the code look to find all records with either of those values?

  8. #23
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    WHERE DEPTS="OA" OR DEPTS="OA_P1" OR DEPTS="OA_P2"

    Or can use IN operator. Review http://allenbrowne.com/ser-50.html

    WHERE DEPTS IN ("OA", "OA_P1", "OA_P2")

    Again, build conditional If Then for each control.
    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.

  9. #24
    Dachbo is offline Advanced Beginner
    Windows 7 32bit Access 2013
    Join Date
    May 2015
    Posts
    42
    he uses a list box to filter, but I have check boxes to filter.

    I came up with an idea:

    Code:
         Dim str_OA As String
        Dim str_OAP1 As String
        
        If Me.chck_OA = -1 Then
            str_OA = "OA"
        
        If Me.chck_OAP1 = -1 Then
             str_OAP1 = "OA_P1"
    
        Me.lstbx_EMPLOYEES.RowSource = "SELECT EMPLOYEES_ID FROM tbl_EMPLOYEES WHERE " & Me.cmbx_EMPLOYEES & "='Y' AND [DEPTS] = 'str_OA' OR [DEPTS] = 'str_OAP1';"
        End If
        End If
    it's not working, but could it work something like this?
    I just recognized, it will skip everything it the first if statement is not true.... so I have to dump that idea

  10. #25
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Consider:
    Code:
    Dim strO As String
    If Me.chck_OA = -1 Then
       strO = strO & "DEPTS = 'OA'"
    End If
    If Me.chck_OAP1 = -1 Then
       strO = strO & IIf(strO = "", "", " OR ") & "DEPTS = 'OA_P1'"
    End If
    strO = IIf(strO = "", "", "(" & strO & ")"
    Me.lstbx_EMPLOYEES.RowSource = "SELECT EMPLOYEES_ID FROM tbl_EMPLOYEES WHERE " & Me.cmbx_EMPLOYEES & "='Y'" & IIf(strOA = "", "", " AND ") & strO & ";"
    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. #26
    Dachbo is offline Advanced Beginner
    Windows 7 32bit Access 2013
    Join Date
    May 2015
    Posts
    42
    thank you for the code,

    unfortunately I get a compile error: "Expected: list separator or)" for this line:

    Code:
    strO = IIf(strO = "", "", "(" & strO & ")"
    but I don't know how to fix it.

    The other question is, how do I add a check box, for example when I have 4 or 5 check boxes and every one could be selected or not
    Last edited by Dachbo; 07-09-2015 at 12:39 AM.

  12. #27
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Sorry for typo. Just need closing paren for the IIf()

    strO = IIf(strO = "", "", "(" & strO & ")")

    If you have more checkboxes, are they for more DEPTS parameters? Just do more If Then structures as shown.
    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. #28
    Dachbo is offline Advanced Beginner
    Windows 7 32bit Access 2013
    Join Date
    May 2015
    Posts
    42
    thank you!

    What does this line exactly do?

  14. #29
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    All it does is concatenate () to the string so the OR series criteria is enclosed in (). Correct parentheticals is critical when combining AND and OR operators in filter.
    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. #30
    Dachbo is offline Advanced Beginner
    Windows 7 32bit Access 2013
    Join Date
    May 2015
    Posts
    42
    ok, thank you. It helped me to understand it and everything is working fine now!!

Page 2 of 2 FirstFirst 12
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