search form

K

kmmuelle

I used Allen Browne's code, however in my database I have many suppliers and
many products. its a many to many relationship between the two. So when i
search for a certain supplier it will show it a bunch of times with each line
being the same except for the product. How can I make it only show the name
once but all of the products?

Thanks
Kristin
 
K

Ken Sheridan

Kristin:

Firstly I assume you have tables such as Suppliers, Products and
ProductSuppliers, the last modelling the many-to-many relationship between
the other two.

Base your form on the Suppliers table and make it single form view. Within
it have a subform, linked to the parent form on SupplierID, based either on
the ProductSuppliers table and using a combo box to show the Product, or on a
query which joins the ProductSuppliers and products table and uses a text box
to show the product.

You can then search the main 'Suppliers' form for a supplier and the subform
will show the products supplied by the supplier. If you want to search by
'products supplied' rather than by the supplier name its a little more
tricky, but if you post back on that I can walk you through how to do it.

Ken Sheridan
Stafford, England
 
K

kmmuelle

Thank you for your help. But I would also like to know how to search by
products supplied
 
K

Ken Sheridan

Kristin:

Firstly the following assumes that you have a table Suppliers with numeric
primary key SupplierID, a table Products with numeric primary key ProductID
and text column Product, and a table ProductSuppliers with two numeric
foreign key columns SuupplierID and ProductID.

To search by product I'd suggest using a multi-select list box, so you can
select one or more products at a time. The results would then be those
suppliers who supply any one of the products selected. You could either put
the list box in the bound suppliers form or in a separate unbound dialogue
form. The following is for the latter, which I think will be less confusing
to the user than trying to combine the search facility with the data display
and editing facilities of a bound form.

First create a form based on the Suppliers table and embed a subform based
on ProductSuppliers or a query joining ProductSuppliers and products as I
described in my first reply. I've assumed below that the parent form is
called frmSuppliers.

Create an unbound dialogue form and add a list box to it. Set the list
box's properties up like so:

Name: lstProducts

RowSource: SELECT ProductID, Product FROM Products ORDER BY Product;

BoundColumn: 1
ColumnCount: 2
ColumnWidths: 0cm;8cm
MultiSelect: Simple

If you are using inches rather than metric units Access will automatically
convert the above ColumnWidths values to inches.

Add a command button to the form for opening the suppliers form, and out the
following code in its Click event procedure:

Const conMESSAGE = "No products selected."

Dim ctrl As Control
Dim varItem As Variant
Dim strProductIDList As String
Dim strCriteria As String

Set ctrl = Me.lstProducts

' loop through list box's ItemsSelected
' collection and build list of ProductIDs
If ctrl.ItemsSelected.Count > 0 Then
For Each varItem In ctrl.ItemsSelected
strProductIDList = strProductIDList & _
"," & ctrl.ItemData(varItem)
Next varItem
' remove leading comma
strProductIDList = Mid(strProductIDList, 2)

' build criteria string for opening form
' with subquery to return all SupplierIDs
' of suppliers who supply any of selected products
strCriteria = "SupplierID IN(" & _
"SELECT Suppliers.SupplierID " & _
"FROM Suppliers INNER JOIN ProductSuppliers " & _
"ON Suppliers.SupplierID=ProductSuppliers.SupplierID " & _
"WHERE ProductID IN (" & strProductIDList & "))"

' open form filtered to relevant suppliers
DoCmd.OpenForm "frmSuppliers", WhereCondition:=strCriteria
Else
MsgBox conMESSAGE, vbExclamation, "Invalid Operation"
End If

Add another button to the form for clearing the selections from the list,
and out the following in its Click event procedure:

Dim ctrl As Control
Dim n As Integer

Set ctrl = Me.lstProducts

' loop through all items in list box
' and de-select if selected
For n = 0 To ctrl.ListCount - 1
ctrl.Selected(n) = False
Next n

You can select any number of items from the list box simply by clicking them
in succession. When the button is then clicked the frmSuppliers form will
open, filtered to show only those suppliers who supply any of the selected
products. The subform will still show all products selected by each
supplier, included in which will be at least one of those you selected in the
dialogue form.

If you implement the above carefully it should work without any trouble, but
if you do have any problems feel free to mail me at:

kenwsheridan<at>yahoo<dot>co<dot>uk

and I can send it to you as a simple little .mdb file.

Good luck,

Ken Sheridan
Stafford, England
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Autofill In Linked Forms 1
Supplier planogram structure 7
Populating a combo box.... 1
cascade combobox and auto populate fields. 11
Lookup Box 8
Automatically filling in a form 1
Setting up a PO Form 2
List Boxes 1

Top