Solved

Filter Based on Value in Subform

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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

785 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