Question : Cross Tab Quert

Dear Experts

I have a query, which comes brings up a simple data set that contains the following fields

Firm (a company)
Name (a person)
Stock (a stock that they follow e.g. Microsoft)

A person can follow a number of stocks & I want to be able to produce a list of stocks that a person follows.  Normally I would use a simple Pivot table to view these, but I need the end result to be output to excel and with all the stocks they follow in one cell.  So for example:

Firm        Name             Stock
HSBC     Joe Bloggs     Microsoft
Citibank  Fred Smith     Microsoft
HSBC     Joe Bloggs     Google
HSBC     Joe Bloggs     Oracle
Citibank  Fred Smith     Oracle

Would end up as:

Firm        Name             Stocks
HSBC     Joe Bloggs     Microsoft, Google, Oracle
Citibank  Fred Smith     Microsoft, Oracle

The reason the data comes in from a linked table is because I need to be able to re use database time and again the therefore the number of firms, people and stocks will change.

Can anyone help with building this query.  The closest I got to solving this was a simple cross tab query which listed the stocks as column headers and put a 1 in the corresponding cell

TRANSFORM Count([Coverage Query].Firm) AS CountOfFirm
SELECT [Coverage Query].Name, Count([Coverage Query].Firm) AS [# Under Coverage]
FROM [Coverage Query]
GROUP BY [Coverage Query].Name
PIVOT [Coverage Query].[Stock];

Many thanks

Answer : Cross Tab Quert

Create a Procedure in a Standard Module:

Public Function Concat(fldName as String) as String
  Dim db as Database, rs as Recordset, strSQL as String
  set db = Currentdb
  strSQL = "SELECT * FROM myQuery WHERE Name = '" & fldName & "'"
  db.openrecordset(strSQL)
  rs.MoveFirst
  Do While Not rs.EOF
    Concat = Concat + rs.Stock + ", "
    rs.MoveNext
  Loop
  Concat = left(Concat,Len(Concat)-2)
  set rs = Nothing
  set db = Nothing
End Function

Call the procedure with this saved query.  Replace myQuery with the actual name of your query in the code above and the SQL below:

SELECT Company, Name, Concat(Name) AS ListOfStocks FROM myQuery
GROUP BY Company, Name;
Random Solutions  
 
programming4us programming4us