applying a filter to a form that has a subform

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bkberg05
    New Member
    • Aug 2009
    • 43

    applying a filter to a form that has a subform

    Hi - I have a form called Vendor (tied to table with same name). Each vendor_id can belong to more than one 'category'. So there's a table called Vendor_Category which contains just vendor_id and category.

    On the Vendor form, there's a subform called Vendor_Category . When a user is in the Vendor form and is in a record for a particular vendor_id, they use a combo_box in the Vendor_Category subform to select Vendor_Categori es that can belong to the Vendor in question.

    That works fine, but now I want to filter by Vendor_Category and just see vendor records the belong to a certain category. So I added an unbound vendor_category combobox which just contains a list of all the different categories. When a user selects one from the list, I've added code in the AfterUpdate that I want to use to set the filter.

    Unfortunately, I have no clue how to write the filter. I know I set the filter and then turn the filter on. But the syntax is a mystery to me.

    Any help would be greatly appreciated.

    Thanks,
    Bill
  • ChipR
    Recognized Expert Top Contributor
    • Jul 2008
    • 1289

    #2
    The Form.Filter Property is just like a Where Condition, so something like:
    Code:
    Me.Filter = "[Category] = '" & cboVendorCategory & "'"
    Me.FilterOn = True

    Comment

    • bkberg05
      New Member
      • Aug 2009
      • 43

      #3
      Hi and thanks,

      The part that's giving me trouble is the [Category] section in your example. When the event runs, it opens up a dialog box asking for the parameter value.

      When I'm putting in the code and I put the field in as [category] with lower space, it doesn't recognize it and change it to uppercase. I thought maybe it was because it was on a subform, but even when I put in a field from the main form, it still doesn't change to upper case.

      Note the following information:
      1. Main form name is "Vendor". It's Record Source is the "Vendor" table. "Vendor_ID" is the primary key.
      2. Sub_form name on the main form is called "Vendor_Categor y_sub". The Source Object for the subform is the form "sub_Vendor_Cat egory". The Link Child and Link Master fields are both "Vendor_ID" .
      3. On the form "sub_Vendor_Cat egory", the Record Source is the "Vendor_Categor y" table which contains only "Vendor_ID" and "Vendor_Categor y" fields. The "Vendor Category" field is what I'm trying to filter on.
      4. The combo box on the "Vendor" form that I'm using to trigger the filter is called "Vendor_Categor y_Filter".

      Just wanted to be thorough. I've checked to see if the [vendor_category] name was duplicated somewhere, but I can't find another instance.

      Thanks again for any future help.

      Comment

      • ChipR
        Recognized Expert Top Contributor
        • Jul 2008
        • 1289

        #4
        [Category] in the code should be replaced with the name of the field in the main form's underlying recordset.

        Comment

        • bkberg05
          New Member
          • Aug 2009
          • 43

          #5
          yes, did that. Here's my actual code

          Me.Filter = "[vendor_category] = '" & Me.Vendor_Categ ory_Filter & "'"
          Me.FilterOn = True

          Comment

          • ChipR
            Recognized Expert Top Contributor
            • Jul 2008
            • 1289

            #6
            If it's still asking for a parameter value, then "vendor_categor y" is not the name of a field in the recordset you are trying to filter.

            Comment

            • bkberg05
              New Member
              • Aug 2009
              • 43

              #7
              Agreed. The field name is definitely "vendor_categor y". But as it's on a sub-form, somehow it must not be part of the recordset? If I change the name to a field on my Vendor form such as "vendor_nam e" for instance, it recognizes it fine.

              Either i'm referring to the field wrong (by not referencing the sub_form name) or the recordset needs to be expanded to include the data in the sub form and i don't know how to do that.

              Comment

              • ChipR
                Recognized Expert Top Contributor
                • Jul 2008
                • 1289

                #8
                To filter the subform, you would use:
                subformControlN ame.Form.Filter = ...

                Comment

                • bkberg05
                  New Member
                  • Aug 2009
                  • 43

                  #9
                  Here's what I wrote. It now says 'Object Required' in the debugger. Maybe I don't know how to reference the [vendor_category] field correctly as it is also in the subform...

                  Vendor_Category _sub.Form.Filte r = "[vendor_category] = '" & Vendor_Category _Filter & "'"
                  Vendor_Category _sub.Form.Filte rOn = True

                  Comment

                  • ChipR
                    Recognized Expert Top Contributor
                    • Jul 2008
                    • 1289

                    #10
                    Make sure that Vendor_Category _sub is the name of the control on the main form.

                    Comment

                    • bkberg05
                      New Member
                      • Aug 2009
                      • 43

                      #11
                      Thanks.

                      Well, it wasn't. Now it is. Now the filter works without an error, but the intended result isn't happening.

                      The sub-form now only shows the category chosen in the filter, but the main form still shows all records. Maybe I didn't explain properly, but I want to only see records on the main form that have a category matching the filter.

                      Comment

                      • ChipR
                        Recognized Expert Top Contributor
                        • Jul 2008
                        • 1289

                        #12
                        I see the problem. You can't filter the main form on the category field because it isn't in the vendor table, which is what the error said. See if you can do:
                        Code:
                        Me.Filter = "[VendorID] IN " _
                          & "(SELECT [VendorID] FROM VendorCategory WHERE " _
                          & "[category] = '" & cboVendorCategory & "')"

                        Comment

                        • bkberg05
                          New Member
                          • Aug 2009
                          • 43

                          #13
                          beautiful !!!!

                          thanks so much!

                          Comment

                          • ChipR
                            Recognized Expert Top Contributor
                            • Jul 2008
                            • 1289

                            #14
                            Glad it works, and sorry I didn't read your initial post more carefully.

                            Comment

                            Working...