Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Concatenate query results into single string

 
   Database Help (Home) -> MS Access RSS
Next:  Need Help with Query  
Author Message
DeZZar

External


Since: Aug 21, 2008
Posts: 41



(Msg. 1) Posted: Thu Jan 01, 2009 9:44 pm
Post subject: Concatenate query results into single string
Archived from groups: comp>databases>ms-access (more info?)

Hi All,

Allen Browne posted the following code in 1998 regarding Concatenating
a string a text from a table into a report.

I want to do something similar. I have a query that returns email
addresses based on some selections made within a form.

I would like to have a txt field on the form that returns the emails
from the query as a single string of text. For example:

bob@hotmail; john@hotmail; frank@hotmail... etc

This is the code that was originally posted and discussed here:
http://groups.google.com.au/group/comp.databases.ms-access/browse_thre...thread/

------QUOTE-------
The function assumes:
1. There is a table that stores the related records you want to
concatenate.
Replace "MyTable" in the function with the actual table name.

2. That table has a foreign key that relates it to the table the
report is
based on.

3. That foreign key field is numeric. Replace "MyForeignKey" in the
function
with the actual field name.

4. Replace "MyField" in the function with the name of the field you
wish to
read. (2 occurances.)

You can then create a text box on your report to display the results.
Make sure this text box's Name is different from any field in your
report's
recordset, and set its ControlSource property to:
=ShowRelated([xxxxxx])
where "xxxxxx" represents the primary key of the report's recordset.
-------------------------------------------------------
Function ShowRelated(vID) As String
'Purpose: Return a single string of all the related records.
Dim db As Database
Dim sSQL As String
Dim rst As Recordset
Dim sOut As String
Dim iLen As Integer
Const SEP = ", " ' The separator between records.


If IsNumeric(vID) Then ' If it's not Null etc.
sSQL = "SELECT MyField FROM MyTable WHERE MyForeignKey = " &
vID &
";"
Set db = CurrentDb()
Set rst = db.OpenRecordset(sSQL)
Do While Not rst.EOF
sOut = sOut & rst![MyField] & SEP
rst.MoveNext
Loop
rst.Close
Set rst = Nothing
Set db = Nothing
iLen = Len(sOut) - Len(SEP)
If iLen > 0 Then
ShowRelated = Left$(sOut, iLen) 'remove trailing
separator.
End If
End If
End Function

-----END QUOTE-----

I have tried to modify this code however I can't seem to get it to
work for me. I think its all coming under around the ForeignKey
crietera which I don't have in my situation.

Has anyone else attempted something similar?

My Query is called "qry_Form_Invitees" and the column I want to
concatenate is "tbl_Bankers.Email" or just 'Email' on the sourse Table

 >> Stay informed about: Concatenate query results into single string 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Concatenate query records into one table field - I have a query (qryProCatJoinDetails) with this format: ProductID CategoryID Specification Prefix Value Suffix 24505 5 Voltage (null) 120 V 24505 5 Current (null) 8 A..

same query getting different results - hello. i'm using ASP (classic) to write a database driven application. it's a really simple app where all i'm doing is pulling data from the db and displaying it in a table. however, i'm having problems with my query. i output the query, copied it,....

Help with complex single-table UPDATE query - I am trying to write an SQL UPDATE statement for an MSAccess table and am having some problems getting my head around it. Can anyone help? TableName: CustTransactions TransactionKey AutoNumber (Primary Key) CustomerID Long Integer (Non-unique..

Joining multiple queries into a single query - Hi, I have the following queries - SELECT Count([tblTestScriptStatus].[StatusType]) AS Count1 FROM tblTestScriptStatus RIGHT JOIN tblTestCase ON tblTestScriptStatus.ID = tblTestCase.TestScriptStatusID WHERE..

graph for query results - I am trying to create graph report with using query results. Query result show me `total quantities` for some related `category name`. When i attempt to create a Chart Report with wizard by using my query, program does not allow me to select `category..
   Database Help (Home) -> MS Access All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]