Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Combine one record per item with multiple records per item

Status
Not open for further replies.

owrsome

Technical User
Jan 13, 2003
18
US
I am building a product database to support a web site, and am having trouble combining fields from two Access queries into a third. The first query contains one record per product while the second query contains multiple records per product. I want the third query to have one record per product, with the information from the second query spread over multiple fields.
The first query, qryProducts, holds specifications for many products. These data are used to populate the web page with information about a product selected by the site visitor. There is one record per product.
The second query, qryClicks, contains data needed to place links on each product page. Fields include Productname, Linktext, URL and Sequence. (The URL’s link to the websites of a product's manufacturer, dealers, etc.) There are a large variety of these links, although usually only 1 – 6 per product.
The two queries are linked via the common Productname field.
I need to provide to my website builder a single Access query, qryProductdata, that contains all data to be placed on a given product’s page. The query should have only one record for a Productname. In this record, the data from the single record in qryProducts will be placed. The multiple records from qryClicks will be spread into multiple fields, e.g. Linktext1, URL1, Linktext2, URL2, up to Linktext20 and URL20. The 1, 2, etc. in the field names refer to the Sequence field in qryClicks. Most of these fields in qryProductdata will be empty, as a given product will have only 1 - 6 links.
To pull the right data from qryClicks into Linktext1, URL1, etc., I have tried placing expressions on the Field line and the Criteria line of qryProductdata’s Design View. The results are either a blank query or a query with multiple lines per product.
I’d be grateful for any help, preferably in terms of the Query Design View in Microsoft Access. I haven’t worked with SQL or VB yet.
 
Can you post your SQL here? It'll be a lot easier just to amend your query than to create a functioning one from scratch. Just open your query in design view, select the "SQL" view and copy/paste the entire statement.

Regards,
makeItSo

Andreas Galambos
EDP / Technical Support Specialist
(andreas.galambos@bowneglobal.de)
HP:
 
If I understand correctly (it helps to type or paste in a few records), there are two FAQs in this form's "How To" that describe how to concatenate multiple child records.

Duane
MS Access MVP
Find out how to get great answers faq219-2884.
 
The SQL for the (simplified) query is
SELECT DISTINCTROW qryBoatCalcs.Boatname, qryBoatCalcs.[Beam-ft], IIf([qryBoatClicks].[Sequence]="1",[qryBoatClicks].[Linktext],"") AS LinkText1, IIf([qryBoatClicks].[Sequence]="1",[qryBoatClicks].,"") AS URL1 FROM qryBoat...oatCalcs.Boatname; Thanks for looking at it.
 
owrsome,
Did I under correctly? Did you check out the two FAQs? Did you try the How To's?

Duane
MS Access MVP
Find out how to get great answers faq219-2884.
 
Does this query not work or does it just not do what you want? If it doesn't do what you want, what does it do wrong?

Why don't you post some sample data and your expected results?
 
Query 1 looks like this (simplified):

BoatID(auto) Boatname(text) Beam(number) Etc.
BoatID(auto) Boatname(text) Beam(number) Etc.
Etc. where each record is a different Boatname


Query 2 looks like this (simplified):

LinkText(text) URL(text) BoatID Sequence(number)
LinkText(text) URL(text) BoatID Sequence(number)
LinkText(text) URL(text) BoatID Sequence(number)
Etc. where there are no duplicate sequence numbers for a given BoatID. The sequence numbers to indicate the position of the link on the web page.

I would like Query 3 to look like this:

BoatName Beam Linktext1 URL1 Linktext2 URL2 Etc.
BoatName Beam Linktext1 URL1 Linktext2 URL2 Etc.
Etc. where each record is a different Boatname. Linktext1 would be the Linktext from Query 2 where Sequence = 1, etc.

I have looked at faq701-4233 and faq701-3499. I am hoping that there is a way to achieve my aim without writing SQL or VB. If not, I will have to learn one or both of them.

Thanks for helping.
 
I doubt there is any way to do this without either SQL or VB. I can't image an alternative that wouldn't include one or both.

Duane
MS Access MVP
Find out how to get great answers faq219-2884.
 
OK. I have searched the Tek-Tips FAQ's and How-To's, which appear to deal with concatenation or the production of a multi-field table, as well as other websites. Unable to find a ready-made solution or explanation of my problem, I have given myself a crash course in SQL.

I've abandoned the IIf function and turned to writing subqueries in SQL. But the closest I have come is

INSERT INTO [Pool] ( Title, Beam, LinkText5 )
SELECT Boats.Boatname AS Title, Boats.[Beam-ft] AS Beam, (SELECT Clicks.Linktext FROM Clicks INNER JOIN Boats ON Clicks.BoatID = Boats.BoatID
WHERE Clicks.BoatID=Boats.BoatID AND INSTR(Clicks.Linktext,"association") >0) AS LinkText5
FROM Boats LEFT JOIN Clicks ON Boats.BoatID = Clicks.BoatID;

Running this gives the error "At most one record can be returned by this subquery." The join properties between Clicks and boats are set to "Include ALL records from 'Boats' . . ."

I'm struggling here.
 
owrsome,
If you use my FAQ about concatenating, your sql might look something like:
SELECT * , Concatenate("SELECT LinkText & ' ' & URL FROM Clicks WHERE BoatID = " & [BoatID] AND Instr(LinkText,'Association') >0 ORDER By Sequence) As LinkTexts
FROM Boats

You would have to add the Concatenate function to your modules. There is a sample of this function's usage at
Duane
MS Access MVP
Find out how to get great answers faq219-2884.
 
Duane,

Many thanks for hanging in with me.

I installed your module and inserted your query. The query produces a syntax error which I am trying to find, but even when I get the query to run, won't it concatenate LinkText and URL in one field? I need each in a separate field.
 
owrsome,
Yes, the field values will be displayed in all one expression/result. If you need multiple fields, then you will need to create a crosstab query. I noticed a sequence field. Is this an incremental sequence number? If you want a crosstab, you will need to number your records for each boatId. This can be done dynamically in the query.

Duane
MS Access MVP
Find out how to get great answers faq219-2884.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top