query assistance- need to combine text

smiles

New Member
Joined
Dec 14, 2004
Messages
17
I'm trying to combine an unknown amount of text together in a query. Basically the opposite of parsing a field.

As an example, let say I have the following table with 2 columns (code and message) with the following records.

code message
----- ------
AA neat
AA great
BB exciting
BB dull
BB neat
CC great

This is the recordset I like to present.

code message
----- -----------
AA neat, great
BB exciting, dull, neat
CC great

Does anyone have a suggestion for accomplishing this?

I'm familiar with crosstabs and know can I do the following:

TRANSFORM Min(TestTable.message) AS MinOfmessage
SELECT TestTable.code
FROM TestTable
GROUP BY TestTable.code
PIVOT "message" & DCount("code","TestTable","message <='" & [message] & "' AND code = '" &
Code:
 & "'");

And get this recordset:

code	message1	message2	message3
----      -----------        -----------         ------------
AA	great	             neat	
BB	dull	               exciting	          neat
CC	great		

Which I can then concatenate by using message1 & iif(message2 <> "",) ", " & message2, "") & iif(message3 <> "",) ", " & message3, "")

The only problem with this crosstab approach is that I won't know how many messsage there are.  There many be 1, 2, 3 or 10.  Is there a more flexible/dynamic method?  Is the JOIN/SPLIT function a possibility?

I appreciate any thoughts!  Thanks in advance for the assistance.
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Ok, this based on the solution at MS KB Article #322813-ACC2000: How to Combine Multiple Child Records into One Record
http://support.microsoft.com/kb/322813

First, create the code-using Alt+F11

Code:
Function CombineChildRecords(strTblQryIn As String, _
strFieldNameIn As String, strLinkChildFieldNameIn As String, _
varPKVvalue As Variant, Optional strDelimiter) As Variant

   Dim db As DAO.Database
   Dim qd As DAO.QueryDef
   Dim rs As DAO.Recordset
   Dim strSQL As String
   Dim varResult As Variant

   Set db = CurrentDb
   Set qd = db.CreateQueryDef("")

   If IsMissing(strDelimiter) Then strDelimiter = "; "
   strSQL = "SELECT [" & strFieldNameIn & "] FROM [" & strTblQryIn & "]"
   qd.SQL = strSQL & " WHERE [" & strLinkChildFieldNameIn & "] = [ParamIn]"
   qd.Parameters("ParamIn").Value = varPKVvalue

   Set rs = qd.OpenRecordset()

   Do Until rs.EOF
     varResult = varResult & rs.Fields(strFieldNameIn).Value & strDelimiter
     rs.MoveNext
   Loop

   rs.Close

   If Len(varResult) > 0 Then varResult = Left$(varResult, _
Len(varResult) - 2)

   CombineChildRecords = varResult

   Set rs = Nothing
   Set qd = Nothing
   Set db = Nothing
End Function

Now create a query, you will need to change the field and table names to match your application.

Code:
SELECT DISTINCT Code, CombineChildRecords("tblNAME","Message","Code",[code*],", ") AS FieldList
FROM tblTest
ORDER BY tblTest.Code;

where code* is actually code but the board won't recognize properly
HTH,
CT
 
Upvote 0
CT

That is sweet code - I learn something new every day! I didn't realise you could devise custom functions for use in Access SQL...

Best regards

Richard
 
Upvote 0
Receiving an error message when running code

CT Witter,

I tried using the code you provided and keep receiving the error message "undefined function 'CombineChildRecords' in expression." Not sure why the SQL does not find the global module provided. Can you help? :)

Thanks!
rturanc
 
Upvote 0
Make sure your module isn't named the same thing 'CombineChildRecords'.

Post back if that doesn't work.

CT
 
Upvote 0
I thought I would reply here rather than my post. Thanks CT Writer, it does look good but I'm not that great at understanding what it's doing. This is my query:

SELECT DISTINCT Code, CombineRecords([TBL00_Rebate Agreements],"Message","Code",[code*],", ") AS FieldList
FROM [TBL00_Rebate Agreements]

When I run some input boxes come up. I'm not sure what needs to go in to them, but I have 76k rows in my table and 9k of them appear to have duplicates.

What does "Message" refer to?
What does "Code" refer to?
What does "Code*" refer to?
 
Upvote 0
Message = strFieldNameIn As String in the function = The name of the field that contains the data that you want.

Code = strLinkChildFieldNameIn As String = The name of the field on which the child records link.

Code* is the same as above, but the forum board didn't like the word code so I changed to code*

If you create the table like smiles listed as the example, you will be able to see how the function works.

HTH,
CT
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,289
Members
452,902
Latest member
Knuddeluff

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top