Using a combobox for searching multiple fields in "option group"

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • LeighW
    New Member
    • May 2012
    • 73

    Using a combobox for searching multiple fields in "option group"

    Hi,

    I'm still having a couple problems with searches.
    I have a search form, frm_Search.
    The form I am trying to filter, frm_Form1
    An unbound combobox on frm_Search, Cbo_Permit
    6 different fields bound to tbl_Table1 within frm_Form1, fld_Permit1, fld_Permit2...

    The 6 bound fields are within a custom made "option group" so that users can select the related permit(s) via check boxes. I understand this is a poor design because of multiple fields in the table but it looks good on the form and each record can have multiple permits.

    What I would like, is to use an unbound combobox within frm_Search, Cbo_Permit, using the names of the fields I shown above. The user then selects a permit within the combobox and presses a command button to open frm_Form1 filtered to that particular permit.

    I need to know if this is possible as it will be one of the main searches users will make.

    With other searches I've used the code:

    Code:
    Private Sub SOMEBUTTON_Click()
    
    Dim MyVar
    
    MyVar = "[SOMEFIELD] = " & SOMECOMBOBOX.Value & "" 'This is the filter
      
    DoCmd.OpenForm "SOMEFORM", WhereCondition:=MyVar 'This opens the form with the filter above.
        
    End Sub
    The problem is, this code only filters by one field and I need to filter by multiple fields.

    Any help would be great,

    Leigh
  • twinnyfo
    Recognized Expert Moderator Specialist
    • Nov 2011
    • 3655

    #2
    Leigh,

    If I understand you correctly, you want the frm_Search to filter records in frm_Form1 based on the value of the combo box in which if someone selects Permit1, then the form will only show records in which Permit1 is used, if the user selects Permit2, then only Permit2 records, and so on.... Is this correct?

    First, it would be nice to know if the permit fields are text fields or Yes/No check boxes. I would recommend making them Yes/No fields, because then this code is incredibly easy. Assuming this is the case, I would remove the Option Group (because I'm not sure it works well covering multiple fields anyway, and replace it with just your six Check Boxes (chkPermit1, chkPermit2, etc.).

    Then, when someone selects any of those check boxes, after they click a command button (we'll call it cmdSearch) the following code fires:

    Code:
    Option Compare Database
    Option Explicit
    
    Private Sub cmdSearch_Click()
    On Error GoTo EH
        Dim strFilter as String
        strFilter = "fld_Permit1 = " & Me.chkPermit1 & _
            "fld_Permit2 = " & Me.chkPermit2 & _
            "fld_Permit3 = " & Me.chkPermit3 & _
            "fld_Permit4 = " & Me.chkPermit4 & _
            "fld_Permit5 = " & Me.chkPermit5 & _
            "fld_Permit6 = " & Me.chkPermit6
        DoCmd.OpenForm "frm_Form1", , , strFilter
        Exit Sub
    EH:
        MsgBox Err.Number & " " & Err.Description
        Exit Sub
    End Sub
    Again, this is my recommendation, that the Permit fields are Yes/No. If not, then you can modify the code above slightly so that you would check to see whether or not there is a value in fld_Permit1, fld_Permit2 etc., based on whther or not the check boxes have been clicked.

    Hope this helps, or at least gets you pointed in the right direction.

    Comment

    • LeighW
      New Member
      • May 2012
      • 73

      #3
      Thank you again twinnyfo.

      The permit fields are yes/no check boxes like you expected. I've changed the combobox in the search form to a set of checkboxes with a command button.

      The only problem is when I click the command button with the code above it comes up with Run-Time Error 3075. Syntax error (missing operator) in query expression. It highlights the Cmd.OpenForm line but I believe it means strFilter.

      Trying to edit some of the code but not working as of yet

      Comment

      • zmbd
        Recognized Expert Moderator Expert
        • Mar 2012
        • 5501

        #4
        Leigh,
        run your code again... when it errors, select the debug option...
        Now with the VBA editor open we can get at that string:
        <ctrl><g> to open the immediate window
        type ?strFilter and press enter
        this will show what the string has evaluated to and you can cut and paste it back here... or it may be obvious what the issue is with it...
        -z

        Comment

        • twinnyfo
          Recognized Expert Moderator Specialist
          • Nov 2011
          • 3655

          #5
          My mistake.... Line 7 should read:

          Code:
          strFilter = "WHERE fld_Permit1 = " & Me.chkPermit1 & _

          Comment

          • LeighW
            New Member
            • May 2012
            • 73

            #6
            No worries but still saying the same thing.

            Code:
            ?strFilter
            WHERE fld_Permit1 = -1fld_Permit2 = fld_Permit3 = 0fld_Permit4 = 0fld_Permit5 = fld_Permit6 =
            That is with chkPermit1 checked when pressing the command button

            Thanks for the help so far though guys
            Last edited by LeighW; Aug 21 '12, 01:18 PM. Reason: Meant chkPermit1

            Comment

            • zmbd
              Recognized Expert Moderator Expert
              • Mar 2012
              • 5501

              #7
              Try changing the code to this...
              you need spaces inbetween each fld_permit*

              Code:
              strFilter = "WHERE ((fld_Permit1 = " & Me.chkPermit1 & _ 
                      ") OR (fld_Permit2 = " & Me.chkPermit2 & _ 
                      ") OR (fld_Permit3 = " & Me.chkPermit3 & _ 
                      ") OR (fld_Permit4 = " & Me.chkPermit4 & _ 
                      ") OR (fld_Permit5 = " & Me.chkPermit5 & _ 
                      ") OR (fld_Permit6 = " & Me.chkPermit6 & "))"
              Once again... if this errors please do the <ctrl><g> thing so we can see how this evaluated.
              -z
              Last edited by zmbd; Aug 21 '12, 01:57 PM. Reason: forgot the "or" and the parentheses - :)

              Comment

              • twinnyfo
                Recognized Expert Moderator Specialist
                • Nov 2011
                • 3655

                #8
                Z- Good catch! I wasn't looking at the details!

                Also, you will need the operator " AND " between all ... Like this:

                Code:
                Option Compare Database 
                Option Explicit 
                  
                Private Sub cmdSearch_Click() 
                On Error GoTo EH 
                    Dim strFilter as String 
                    strFilter = "fld_Permit1 = " & Me.chkPermit1 & _ 
                        " AND fld_Permit2 = " & Me.chkPermit2 & _ 
                        " AND fld_Permit3 = " & Me.chkPermit3 & _ 
                        " AND fld_Permit4 = " & Me.chkPermit4 & _ 
                        " AND fld_Permit5 = " & Me.chkPermit5 & _ 
                        " AND fld_Permit6 = " & Me.chkPermit6 
                    DoCmd.OpenForm "frm_Form1", , , strFilter 
                    Exit Sub 
                EH: 
                    MsgBox Err.Number & " " & Err.Description 
                    Exit Sub 
                End Sub

                Comment

                • zmbd
                  Recognized Expert Moderator Expert
                  • Mar 2012
                  • 5501

                  #9
                  I'm also thinking that you may need to default the checkboxes (me.chkpermit#) to false... depends on how the table is setup, are the fields null, true, or false, that we're looking at... this might be a better build to use the afterupdate event in each checkbox to help build the string...

                  -z

                  Comment

                  • LeighW
                    New Member
                    • May 2012
                    • 73

                    #10
                    After trying Z's method I get the same error just more syntax:

                    Code:
                    ?strFilter
                    WHERE ((fld_Permit1 = -1) OR (fld_Permit2 = ) OR (fld_Permit3 = ) OR (fld_Permit4 = ) OR (fld_Permit5 = ) OR (fld_Permit6 = ))
                    Twinnyfo: I'm not sure what you mean by the AND when the OR has been placed there using Z's method? Do mean place the words AND OR together?

                    Z: The bound fields are checked either true or false on frm_Form1 and on the search form I have an unbound set of checkboxes which should also be true or false but I guess they could also be "null"
                    Aha that may be the problem.

                    Comment

                    • LeighW
                      New Member
                      • May 2012
                      • 73

                      #11
                      Nay set all the default values to =False but still but working

                      Comment

                      • twinnyfo
                        Recognized Expert Moderator Specialist
                        • Nov 2011
                        • 3655

                        #12
                        Depends on if you want records that meet all criteria or those that meet at least one of the criteria.

                        Comment

                        • LeighW
                          New Member
                          • May 2012
                          • 73

                          #13
                          The user should only selects one value when using that search method although I guess if the user really wants to filter it by more than one permit they have the option to with "AND"

                          Comment

                          • LeighW
                            New Member
                            • May 2012
                            • 73

                            #14
                            OK I've set the defaults to "False" on Form1 too and now it is coming up with a different error message: Run-Time Error 3085. Undefined function 'WHERE' in expression

                            Comment

                            • zmbd
                              Recognized Expert Moderator Expert
                              • Mar 2012
                              • 5501

                              #15
                              Leigh,
                              Let's go back to what this form is trying to accomplish and the undrlying table structure.
                              -z

                              Comment

                              Working...