• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 343
  • Last Modified:

Filter Based on Value in Subform

We have a primary form of products with a Subform called Products_Pricing that is a one-to-many.  In the subform there is a filter that has the CustomerID.  How can we filter the primary form based on the Subform value???  

For example, there are 500 total products, but some products are sold to Customer A and some are sold to CustomerB, whereas some are sold to both customers.  Our subform has a customer field.  If the users use the built in filter, then it just filters the customers within the "one product".  So if CustomerA has pricing set for 40 products, they want to be able to say just show me Customer A Active Products.
0
sharpapproach
Asked:
sharpapproach
  • 2
  • 2
1 Solution
 
dqmqCommented:
It sounds like the subform is constrained by the main form -- probably with a master-child relationship.  That's a common design and very powerful, at that, because it does a lot of things automatically (read that: no programming required).  If do not want the sub-form constrained by the main form, then you need to eliminate the master-child relationship that is declared in the properties of the subform control.   Understand though, in so doing, you lose the automated nature of the master-child relationship.   Specifically, you need to programmatically constrain the child form to the parent form if you want that behavior or programmatically filter the child form on customer without regard for the context of the main form if that is the behavior you want.  

Rather than using filters, I would steer you toward manipulating the recordsource of the parent form based on the values selected in the subform.  It is much the way you might manipulate the recordsource of the parent form based on a combo box selection.
0
 
peter57rCommented:
It doesn't seem that you can Filter the main form, because (I am guessing) there would not be a customer value available to use in a filter.
So this becomes a little more tricky.

When you want products for a given customer, you will have to use code in the subform to change the recordsource for the main form to a query which will be something like..

Dim strsql as string
strsql = "Select Products.* from Products as P Inner Join [Products_Pricing] as PP "
strsql = strsql & " On P.ProductId= PP.ProductID "
strsql = strsql & " Where PP.CustomerID= " & me.Customerid
me.parent.recordsource = strsql

In my opinion, you should expect users to want to filter or not filter the subform, independently of whether the main form is 'filtered' or not so you should use a button on the subform to trigger the main form change, using the code above.  (the code I indicated will not work in the main form).

And I expect you will also need a button on your main form to show all products again.

Me.recordsource = "Products"
0
 
Jeffrey CoachmanCommented:
<How can we filter the primary form based on the Subform value???  >
As Pete states, trying to filter a main form from a value in a sub form is not commonly done.
You are basically asking too much of the basic main/subform design.
If your Main form is Products and your subform is "Product Pricing", then you could create another form (Pop-up form or a sub-subform) to display specific pricing for a certain customer.

Or, better, ...create a main form for the customer, then filter that customer's pricing in a subform.
0
 
Jeffrey CoachmanCommented:
...just a note.

Your system here would also need a facility for tracking "Price History...

Note that as prices change, in some cases you will need to look up the "Old" price, in other cases you will need the "Current Price"...

...but you may want to post this as a separate new question...

JeffCoachman
0
 
dqmqCommented:
To be clear,,,let's suppose you have a main form that shows all products.  When you select a product, a subform displays all pricings (and implicitly customers) for the selected product.  Now, you want to select one of the customers and then refresh the main form with only products associated with that customer.  

I think that's achievable.

1.  add an (invisible?) llistbox to the main form to hold customerid

2.  Change the record source of the main form to a query having this form:
 select * from products p where exists
    (select * from products_pricing pp where pp.productID = p.productID and pp.customerID = nz(yourcustomerlistbox,pp.customerid)

At this point, your main form should work the same as it does now.

3. In the on_current event of the subform, add some code like this:
   yourcustomerListbox=me.customerid
   yourmainform.requery

Now the mainform should be reduced to only products by the selected customer

4. You may need a few other lines to polish things up.  Like re-establishing the selected product and customer.  Like starting over with all products, etc.  But let's get to those refinements later after you have the basic concept working.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now