Solved

Filter Based on Value in Subform

Posted on 2012-04-01
5
329 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now