?
Solved

Access SQL get concatenation from a field of depending records;

Posted on 2012-12-31
4
Medium Priority
?
314 Views
Last Modified: 2013-01-02
Hi experts,

I have a problem and can't even find a good search words to make a descent search. So I'll describe a context better hoping, that you can help me.

I've 2 tables: tbl1 and tbl2 with 1 to N dependency between them (1 bundle contract can contain N specific contracts):

tbl1:

bundleContractID
bundleContractAddress

tbl2:

specContractID
bundleContractID
specContractAddress

So I need a query, that gives me 2 fields back:

bundleContractID
(all specContractAddress values to the bundleContractID above).

Example ('--' is a field separator):

tbl1

bundleContractID -- bundleContractAddress
1 -- "Adr1"

tbl 2
specContractID -- bundleContractID -- specContractAddress
1 -- 1 -- "Adr2"
2 -- 1 -- "Adr3"
...
N -- 1 -- "AdrN"

N may vary from bundleContract to bundleContract.. And I don't know in general case,
if there is only 1 Adress or many of them, that fit to each bundleContract.

Query expected results:

bundleContractID -- specAdrSummary
1 -- "Adr2, Adr3, .. AdrN"

So I need to have a kind of a loop on depending records and concatenate the values in a specific field in one SQL expression.

How can it be done? Do you have a tip?

Thanx in advance!

Yuriy

P.S. If you have the solution in any other SQL-Language (DB2-SQL, MySQL, etc.), it's OK, I'll map it then somehow on Acccess SQL.
0
Comment
Question by:inversojvo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 320 total points
ID: 38732400
in mysql, it exists: group_concat.
here a thread for a alternative for ms access:
http://stackoverflow.com/questions/2852892/is-there-a-group-concat-function-in-ms-access
0
 
LVL 48

Assisted Solution

by:Dale Fye
Dale Fye earned 760 total points
ID: 38732418
I use a function for this:

To use this, your query would read something like:
SELECT Table1.bundleContractID,
     fnConcat("specContractAddress", "table2",,,"[bundleContractID] = " & Table1.bundleContractID)
FROM Table1

Open in new window

As you can see, the function provides several options including:
Delimeter - character or characters to separate the various elements
Wrapper - character to put before and after each element of the result set.  I use this to wrap strings in quotes or single quotes
Criteria - Determines which records to combine
UniqueValues - If true, the result set will exclude duplicates
Public Function fnConcat(FieldName As String, TableName As String, _
                         Optional Delimeter As String = ",", _
                         Optional Wrapper As String = "", _
                         Optional Criteria As Variant = Null, _
                         Optional UniqueValues As Boolean = False) As String

    Dim strSQL As String
    Dim rs As DAO.Recordset
    Dim varConcat As Variant
    
    strSQL = "SELECT " & IIf(UniqueValues, "DISTINCT", "") & " [" & FieldName & "] " _
           & "FROM [" & TableName & "] " _
           & ("WHERE " + Criteria)
    Set rs = CurrentDb.OpenRecordset(strSQL, , dbFailOnError)
    
    varConcat = Null
    While Not rs.EOF
        If Not IsNullOrBlank(rs(0)) Then
            varConcat = (varConcat + Delimeter) & (Wrapper + rs(0) + Wrapper)
        End If
        rs.MoveNext
    Wend

ConcatExit:
    fnConcat = Nz(varConcat, "")
    If Not rs Is Nothing Then
        rs.Close
        Set rs = Nothing
    End If
    Exit Function
    
ConcatError:
    Call MsgBox("Something failed in the concatenation function")
    Resume ConcatExit
        
End Function

Open in new window

0
 
LVL 5

Accepted Solution

by:
RehanYousaf earned 920 total points
ID: 38732540
0
 

Author Closing Comment

by:inversojvo
ID: 38736160
Thank you very much, guys, that was what I've looked for. <br /><br />The first answer was a pointing a direction, the 2nd and 3rd have had the necessary code, the 3rd one was the most complete, so I hope you're OK with the points distribution.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
Suggested Courses

771 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