Solved

Filter Based on Value in Subform

Posted on 2012-04-01
5
332 Views
Last Modified: 2012-05-06
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
Comment
Question by:sharpapproach
  • 2
  • 2
5 Comments
 
LVL 42

Expert Comment

by:dqmq
ID: 37794031
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
 
LVL 77

Expert Comment

by:peter57r
ID: 37794886
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
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 37796322
<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
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 37796338
...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
 
LVL 42

Accepted Solution

by:
dqmq earned 500 total points
ID: 37796858
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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question