tags:

views:

93

answers:

3

Hello,

I'm making a database in Access 2007, and currently I'm struggling with a query. In only one column, it is possible to cose multiple answers. So, when I open the query I get 3 rows when 3 answers were given. I would like to concatenate the rows into one. Sometimes there is no answer, sometimes 1 or 2 or maximum 3.

For the moment my query looks like this:

COLUMNNAMES: B - Latijnse benaming (space) B - Nederlandse benaming (space) Bloemen.B - April 2.Value (space) B - Klas (space) - B - Leerkracht (space) B - Locatie

ROW 1 Abelmochus (space) (space) Zaaien (space) 1ATA (space) Depuydt (space) Serre 1

ROW 2 Abelmochus (space) (space) Oogsten (space) 1ATA (space) Depuydt (space) Serre 1

ROW 3 Abelmochus (space) (space) Bemesten (space) 1ATA (space) Depuydt (space) Serre 1

I would like it when it looks like this:

ROW 1 Abelmochus (space) (space) Zaaien, Oogsten, Bemesten (space) 1ATA (space) Depuydt (space) Serre 1

The tbl is called Bloemen and this is the SQl of the query for the moment:

  SELECT Bloemen.[B - Latijnse benaming], Bloemen.[B - Nederlandse benaming], Bloemen.[B - April 2].Value, Bloemen.[B - Klas], Bloemen.[B - Leerkracht], Bloemen.[B - Locatie]
  FROM Bloemen
  WHERE (((Bloemen.[B - April 2].Value) Is Not Null));

Can anyone help me to concatenate the rows into one. I don't know how to write a SQL... I have to do this for 48 querys, so I would be helpfull when it's kind of standard.

Thanks a lot!!!!!!!

Jonas Roelens

+1  A: 

"Standard" SQL won't do this, I think. There may be some features in Access to return an array with column values... but unless someone else comes up with a cool suggestion based on such a capability, I'd say "no".

This kind of processing normally has to be done in the programming environment that called the SQL. Are you using some kind of report generator such as BIRT or are you doing "straight" JDBC? If the latter, then you can simply loop through your ResultSet to put that compound column together.

Carl Smotricz
Thanks for the answer but it's still not clear to me how I can fix this... I found other questions on this site who are a bit simular but my question seemed a bit different (to me).
Remou's answer implements what I was talking about, though in a horrible language ;) If you need help "seeing" this in Java, give us a shout and someone will translate for you.
Carl Smotricz
+1 Not only does Standard SQL not have a concatenate set function but the Access Database Engine doesn't have one either, and for good reason too...
onedaywhen
@Carl Smotric: You may not like VBA, but if the user is working within Access, it's the obvious choice. Given that the original question says "I'm making a database in Access 2007" it seems pretty obvious that VBA is the most appropriate language to code the solution in.
David-W-Fenton
@David W Fenton: Oops, my mistake. I was sure I'd seen "Java" somewhere in the question's text. I remember thinking it was a bit unusual for someone to be accessing Access from Java but was determined to help him anyway. By the same token, I assumed he might not understand VBA. My response was perfectly appropriate but based on a hallucination. Sorry to anyone I may have annoyed!
Carl Smotricz
A: 

How about:

The code sample below will return a delimited list from either SQL or string input. This is how to use it in a query:

   SELECT documents.MembersOnly, 
       ConcatList("SELECT FName From Persons WHERE Member=True",":") AS Who, 
       ConcatList("",":","Mary","Joe","Pat?") AS Others
   FROM documents;

Or

   SELECT tc.Company, 
          ConcatList("SELECT Contract 
                      FROM tblservices 
                      WHERE CompanyKey=" & tc.[CompanyKey],", ") AS Contracts
   FROM tblCompanies tc

Sample Code

   Function ConcatList(strSQL As String, strDelim, ParamArray NameList() As Variant)
   ''Reference: Microsoft DAO x.x Object Library
   Dim db As Database
   Dim rs As DAO.Recordset
   Dim strList As String

   Set db = CurrentDb

   If strSQL <> "" Then
       Set rs = db.OpenRecordset(strSQL)

       Do While Not rs.EOF
           strList = strList & strDelim & rs.Fields(0)
           rs.MoveNext
       Loop

       strList = Mid(strList, Len(strDelim) + 1)
   Else

       strList = Join(NameList, strDelim)
   End If

   ConcatList = strList

   End Function

From: http://wiki.lessthandot.com/index.php/Concatenate%5Fa%5FList%5Finto%5Fa%5FSingle%5FField%5F%28Column%29

Remou
An ADODB Recordset has built in support for this e.g. strList = rs.GetString(RowDelimeter:=strDelim)
onedaywhen
@onedaywhen: why don't you make the ADODB suggestion a separate answer? It's an awfully good idea, and one of those cases where ADO offers something useful that DAO lacks.
David-W-Fenton
I've been here before with @Remou: if I edit @Remou's answer to add an ADO answer he takes umbrage and rolls back; if I post a new answer, @Remou takes umbrage and down votes it. On the other hand, a comment can't be down voted or edited. This is not the way I'd like SO to be but that's how it pans out in with some people :(
onedaywhen
@onedaywhen You mistake me. I do not vote down new answers, I very rarely vote down (<14% of votes). If you edit in keeping with the original answer and in the same style of language, I do not roll back, and if it is not in the same style, I will change it to suit. However, at times you have edited in contradiction to the original answer and very rarely does your style match.
Remou
A: 

Thanks to everyone who has answered my question.

Did you get a solution? Did something someone suggesting provide you with it? If so, you need to accept that answer.
David-W-Fenton