473,394 Members | 1,640 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

How to join data from one table field together to output as a string

I have a table where we hold ID's for a particular customer and for each year the customer renews the contract we use the same number. I need to be able to output as a single string all the unique ID's with a semi colon inbetween for transfer into another system but I do not know how to do this in a query.
I'm assuming the first stage would be to build a query that groups the ID's together but how to join them together after that is the problem.
Example:
Policy 1 2014 ID1
Policy 1 2015 ID1
Policy 2 2015 ID2
Policy 3 2014 ID3
Policy 3 2015 ID3
becomes ID1;ID2;ID3
May 18 '15 #1
1 1110
jforbes
1,107 Expert 1GB
On SQL Server there are a couple ways to do this, but there is not an easy, strictly SQL way of doing this in Access. In my experience in Access, a function needs to be used.

You can check out these threads for a solution:
http://bytes.com/topic/access/answer...opposite-union
and http://bytes.com/topic/access/answer...nto-one-result

An alternate solution I've used, which is similar to the ones above, is the following function:
Expand|Select|Wrap|Line Numbers
  1. Public Function getRowsIntoColumn(ByVal sTable As String, ByVal sColumn As String, ByVal sCriteria As String, ByVal sDelimiter As String, Optional bDistinct As Boolean = False, Optional iMaxLength As Integer = 0)
  2.  
  3.     ' Iterates through all the records of the specified table,
  4.     ' using the sCriteria as a where clause
  5.     ' and concatenates the specified columns into a single string
  6.  
  7.     Dim sSQL As String
  8.     Dim oRst As DAO.Recordset
  9.     Dim sNewText As String
  10.     Dim sReturn As String
  11.  
  12.     sSQL = sSQL & "SELECT "
  13.     If bDistinct Then sSQL = sSQL & "DISTINCT "
  14.     sSQL = sSQL & sColumn & " FROM " & sTable & " WHERE " & sCriteria
  15.     Set oRst = CurrentDb.OpenRecordset(sSQL, dbOpenForwardOnly)
  16.     Do While Not oRst.EOF
  17.         sNewText = Nz(oRst(sColumn), "")
  18.         If Len(sNewText) > 0 Then sReturn = sReturn & sNewText & sDelimiter
  19.         oRst.MoveNext
  20.     Loop
  21.  
  22.     If Len(sReturn) > 0 Then sReturn = Left(sReturn, Len(sReturn) - Len(sDelimiter))
  23.     If iMaxLength > 0 And Len(sReturn) > iMaxLength Then sReturn = Left(sReturn, iMaxLength)
  24.  
  25.     getRowsIntoColumn = sReturn
  26. End Function
To use this, build a Query something like this:
Expand|Select|Wrap|Line Numbers
  1. SELECT CustomerID, CustomerName, getRowsIntoColumn("Policies", "PolicyID", "CustomerID='" & [CustomerID] & "'", ", ", True) AS Policies FROM Customers
May 18 '15 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: vool | last post by:
Hi All Can anyone help with this please. I need a way of putting say 10 to 20 bullet points in one table field in an Access database - say seperate them with a special character, then build a...
1
by: Martin Heal | last post by:
I am having problems trying to assign the contents of a Memo table field to a String variable in VB using the following code: Private Function udfBuildSQL(SQLID As Integer) As Variant Dim rs...
4
by: Onion | last post by:
This has to be simple, but I'm forced to admit that I'm a novice who can't figure it out. I have a listbox in a form that allows multiple selections. That works fine. The problem: I can't...
2
by: .Net Sports | last post by:
I have a data grid that takes data from a data table, and in visualstudio.net, I cannot find the error in the debugger at why I am not able to see my datagrid when querying data for a specific date...
2
by: Greg Strong | last post by:
Hello All, Is it possible to change table field lookup properties in code? I've been able to change other field properties in code, however so far no luck with field lookup properties. What...
2
blhuff
by: blhuff | last post by:
I would like to know if there is some code and/or function that will give me a way of retrieving a certain text string from a data table that I have established in MS Access. The text in question is...
3
by: imtmub | last post by:
Hi, I am trying to link sql table field and excel sheet column. SELECT wko_itemid, imc_qty, ima_price FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0', 'Excel...
1
by: peridian | last post by:
Hi, For some daft reason, the database I've been asked to look at stores a series of tickboxes from a website into a single field, with a delimiter separating them. E.g. RecordID |...
3
by: jeanydoggy | last post by:
I need to use a variable value as a table field name. But when I ran the code, it gave me error and didn't use variable value but used the variable as the field name. I defined the following Sub and...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.