473,394 Members | 1,746 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.

Build comma-delimited list from table

I have a table that looks like this:

Forest Residents
Black Josh
Black Joellen
Black Mary Jane
Brown Gertrude
Brown Josh
Brown Mary Jane
I've seen some UDFs for SQL Server to build a comma-delimited list, but
I need a function to use entirely in Access to build the following:

Residents Forests
Josh Black, Brown
Joellen Black
Mary Jane Black, Brown
Gertrude Brown

The table above indicates the forests each person has visited in a
comma-delimited list.

Any suggestions?

Nov 13 '05 #1
22 13615
Per ar****@yahoo.com:
Residents Forests
Josh Black, Brown
Joellen Black
Mary Jane Black, Brown
Gertrude Brown

The table above indicates the forests each person has visited in a
comma-delimited list.


If I were doing it, I'd open up the source table sorted by Person/Forest and
just iterate through the table, concatenating PersonName|Tab|ForestName into a
string variable - and flushing the string to disk each time PersonName changes
(not forgetting to flush the last one at .EOF)
--
PeteCresswell
Nov 13 '05 #2
"(PeteCresswell)" <x@y.z.invalid.USA> wrote in
news:u2********************************@4ax.com:
Per ar****@yahoo.com:
Residents Forests
Josh Black, Brown
Joellen Black
Mary Jane Black, Brown
Gertrude Brown

The table above indicates the forests each person has visited
in a comma-delimited list.


If I were doing it, I'd open up the source table sorted by
Person/Forest and just iterate through the table,
concatenating PersonName|Tab|ForestName into a string variable
- and flushing the string to disk each time PersonName changes
(not forgetting to flush the last one at .EOF)


That's exactly how Dev Ashish wrote the fConcatChild function
http://www.mvps.org/access/modules/mdl0004.htm

Why reinvent the wheel?

--
Bob Quintal

PA is y I've altered my email address.
Nov 13 '05 #3

ar****@yahoo.com wrote:
I have a table that looks like this:

Forest Residents
Black Josh
Black Joellen
Black Mary Jane
Brown Gertrude
Brown Josh
Brown Mary Jane
I've seen some UDFs for SQL Server to build a comma-delimited list, but
I need a function to use entirely in Access to build the following:

Residents Forests
Josh Black, Brown
Joellen Black
Mary Jane Black, Brown
Gertrude Brown

The table above indicates the forests each person has visited in a
comma-delimited list.

Any suggestions?

Firstly, I'm assuming their is a "ResidentID" field in your many table
above and it looks as follows:

ResidentID Forest Residents
1 Black Josh
2 Black Joellen
3 Black Mary Jane
4 Brown Gertrude
1 Brown Josh
3 Brown Mary Jane

If so, put the following function (a modified version of one by Allen
Browne) into a module:

Function ConcatDetail(Num As Long) As Variant
Dim MyDB As Database
Dim rst As Recordset
Dim strOut As String
Dim strSql As String
Dim lngLen As Long

Const strcSep = ","

Set MyDB = CurrentDb()

'Change Table1 to your table's name
strSql = "SELECT Forest FROM Table1 WHERE ResidentID = " & Num & ";"
Set rst = MyDB.OpenRecordset(strSql)
With rst
Do While Not .EOF
strOut = strOut & !Forest & strcSep
.MoveNext
Loop
End With
rst.Close

lngLen = Len(strOut) - Len(strcSep)
If lngLen > 0 Then
ConcatDetail = Left(strOut, lngLen)
Else
ConcatDetail = Null
End If

Set rst = Nothing
Set MyDB = Nothing
End Function
Next, Create a query on the table (Your 1st table shown above and
called Table1 in this example). Add the "Resident" field and create
another field beside this e.g.

Forests: ConcatDetail([ResidentID])

Run this query and you should see what you require.

osmethod

Nov 13 '05 #4
Per Bob Quintal:
That's exactly how Dev Ashish wrote the fConcatChild function
http://www.mvps.org/access/modules/mdl0004.htm

Why reinvent the wheel?


I must've misunderstood the OP.

I thought all the data were in a single table - with multiple records for each
person depending on how many forests.
--
PeteCresswell
Nov 13 '05 #5
You should also set "Unique Values" to yes in the query.....

osmethod

Nov 13 '05 #6
"(PeteCresswell)" <x@y.z.invalid.USA> wrote in
news:os********************************@4ax.com:
Per Bob Quintal:
That's exactly how Dev Ashish wrote the fConcatChild function
http://www.mvps.org/access/modules/mdl0004.htm

Why reinvent the wheel?


I must've misunderstood the OP.

I thought all the data were in a single table - with multiple
records for each person depending on how many forests.


That's exactly what fConcatChild() expects. It goes thrugh the
table, adding the value from each record to a list, where the
filter would be based on the person field.

--
Bob Quintal

PA is y I've altered my email address.
Nov 13 '05 #7
Yes I am trying to implement this With the source data all in one table
..I'm not sure how to implement this given the info you provided.
Bob Quintal wrote:
"(PeteCresswell)" <x@y.z.invalid.USA> wrote in
news:os********************************@4ax.com:
Per Bob Quintal:
That's exactly how Dev Ashish wrote the fConcatChild function
http://www.mvps.org/access/modules/mdl0004.htm

Why reinvent the wheel?


I must've misunderstood the OP.

I thought all the data were in a single table - with multiple
records for each person depending on how many forests.


That's exactly what fConcatChild() expects. It goes thrugh the
table, adding the value from each record to a list, where the
filter would be based on the person field.

--
Bob Quintal

PA is y I've altered my email address.


Nov 13 '05 #8
"(PeteCresswell)" <x@y.z.invalid.USA> wrote in
news:u2********************************@4ax.com:
Per ar****@yahoo.com:
Residents Forests
Josh Black, Brown
Joellen Black
Mary Jane Black, Brown
Gertrude Brown

The table above indicates the forests each person has visited in a
comma-delimited list.


If I were doing it, I'd open up the source table sorted by
Person/Forest and just iterate through the table, concatenating
PersonName|Tab|ForestName into a string variable - and flushing
the string to disk each time PersonName changes (not forgetting to
flush the last one at .EOF)


Why not just SELECT DISTINCT, then you don't have to code to check
when the name changes?

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 13 '05 #9
os******@eircom.net wrote in
news:11**********************@f14g2000cwb.googlegr oups.com:
You should also set "Unique Values" to yes in the query.....


And you should also probably remove the ones that have Null or
zero-length string in the field you're concatenative, or you'll get
consecutive commas.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 13 '05 #10
ar****@yahoo.com wrote in
news:11*********************@z14g2000cwz.googlegro ups.com:
Yes I am trying to implement this With the source data all in
one table .I'm not sure how to implement this given the info
you provided.


build a query to select the distinct persons and the function in
the second column.

Bob Quintal wrote:
"(PeteCresswell)" <x@y.z.invalid.USA> wrote in
news:os********************************@4ax.com:
> Per Bob Quintal:
>>That's exactly how Dev Ashish wrote the fConcatChild
>>function http://www.mvps.org/access/modules/mdl0004.htm
>>
>>Why reinvent the wheel?
>
> I must've misunderstood the OP.
>
> I thought all the data were in a single table - with
> multiple records for each person depending on how many
> forests.


That's exactly what fConcatChild() expects. It goes thrugh
the table, adding the value from each record to a list, where
the filter would be based on the person field.

--
Bob Quintal

PA is y I've altered my email address.


--
Bob Quintal

PA is y I've altered my email address.
Nov 13 '05 #11
good point...
David W. Fenton wrote:
os******@eircom.net wrote in
news:11**********************@f14g2000cwb.googlegr oups.com:
You should also set "Unique Values" to yes in the query.....


And you should also probably remove the ones that have Null or
zero-length string in the field you're concatenative, or you'll get
consecutive commas.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc


Nov 13 '05 #12
I set up the tablet and function in a module, but the module does not
seem to be doing anything.

Nov 13 '05 #13
ar****@yahoo.com wrote:
I set up the tablet and function in a module, but the module does not
seem to be doing anything.


Have you called it anywhere? Code in modules doesn'tjust execute on its own.

--
I don't check the Email account attached
to this message. Send instead to...
RBrandt at Hunter dot com
Nov 13 '05 #14

ar****@yahoo.com wrote:
I set up the tablet and function in a module, but the module does not
seem to be doing anything.


Sorry, but did you try my post earlier. I setup a table as you showed
and tested it... it shows exactly what you requested to see...?

osmethod

Nov 13 '05 #15
I created a query and added the residentID column and a new column as
you suggested, calling the function in the Forests:
ConcatDetail([ResidentID]) column in the query. Do I need to do
anything else?

osmet...@eircom.net wrote:
ar****@yahoo.com wrote:
I set up the tablet and function in a module, but the module does not
seem to be doing anything.


Sorry, but did you try my post earlier. I setup a table as you showed
and tested it... it shows exactly what you requested to see...?

osmethod


Nov 13 '05 #16
ar****@yahoo.com wrote:
I created a query and added the residentID column and a new column as
you suggested, calling the function in the Forests:
ConcatDetail([ResidentID]) column in the query. Do I need to do
anything else?

osmet...@eircom.net wrote:
ar****@yahoo.com wrote:
I set up the tablet and function in a module, but the module does not
seem to be doing anything.


Sorry, but did you try my post earlier. I setup a table as you showed
and tested it... it shows exactly what you requested to see...?

osmethod


Step 1
Add "ResidentID" field to your many table and your table should now
look as follows:

ResidentID Forest Residents
1 Black Josh
2 Black Joellen
3 Black Mary Jane
4 Brown Gertrude
1 Brown Josh
3 Brown Mary Jane
Step 2
Create a separate new vba module and put the following vba function
into this module,
(Copy the following code and paste it into your module)

'---Copy from Here
Function ConcatDetail(Num As Long) As Variant
Dim MyDB As Database
Dim rst As Recordset
Dim strOut As String
Dim strSql As String
Dim lngLen As Long
Const strcSep = ","
Set MyDB = CurrentDb()
'Change Table1 to your table's name
strSql = "SELECT Forest FROM Table1 WHERE ResidentID = " & Num & ";"
Set rst = MyDB.OpenRecordset(strSql)
With rst
Do While Not .EOF
strOut = strOut & !Forest & strcSep
.MoveNext
Loop
End With
rst.Close
lngLen = Len(strOut) - Len(strcSep)
If lngLen > 0 Then
ConcatDetail = Left(strOut, lngLen)
Else
ConcatDetail = Null
End If
Set rst = Nothing
Set MyDB = Nothing
End Function
'---To Here

Step 3
Create a new query on the table (your many table (Step 1) above and
called "Table1" in the code in (Step 2). (Change the name of the table
in the code to the actual name of your table).

Add the "Resident" field to the Query and create
another new field beside this e.g. Put the Following in a "Field" in
the query...

Forests: ConcatDetail([ResidentID])

With the query, set "Unique Values" to yes.

Step 4
Run this query and you should see what you require.
osmethod

Nov 13 '05 #17
On this line

Dim MyDB As Database

I am getting an error stating: User-defined type not defined.

Is there a problem with the "As Database" portion?

Nov 13 '05 #18
What version of Access? Database is a DAO object. By default, Access 2000
and Access 2002 both use ADO, with no reference preset to DAO.

With any code module open, select Tools | References from the menu bar,
scroll through the list of available references until you find the one for
Microsoft DAO 3.6 Object Library, and select it. If you're not going to be
using ADO, uncheck the reference to Microsoft ActiveX Data Objects 2.1
Library

If you have both references, you'll find that you'll need to "disambiguate"
certain declarations, because objects with the same names exist in the 2
models. For example, to ensure that you get a DAO recordset, you'll need to
use Dim rsCurr as DAO.Recordset (to guarantee an ADO recordset, you'd use
Dim rsCurr As ADODB.Recordset)

The list of objects with the same names in the 2 models is Connection,
Error, Errors, Field, Fields, Parameter, Parameters, Property, Properties
and Recordset
--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no e-mails, please!)

<ar****@yahoo.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
On this line

Dim MyDB As Database

I am getting an error stating: User-defined type not defined.

Is there a problem with the "As Database" portion?

Nov 13 '05 #19
"Douglas J. Steele" <NOSPAM_djsteele@NOSPAM_canada.com> wrote in
news:tb********************@rogers.com:
With any code module open, select Tools | References from the menu
bar, scroll through the list of available references until you
find the one for Microsoft DAO 3.6 Object Library, and select it.
If you're not going to be using ADO, uncheck the reference to
Microsoft ActiveX Data Objects 2.1 Library

If you have both references, you'll find that you'll need to
"disambiguate" certain declarations, because objects with the same
names exist in the 2 models. For example, to ensure that you get a
DAO recordset, you'll need to use Dim rsCurr as DAO.Recordset (to
guarantee an ADO recordset, you'd use Dim rsCurr As
ADODB.Recordset)


I think it's good practice to disambiguate regardless of which
library you're using.

But the default A2K mdb stupidly doesn't have a DAO reference at
all.

So, my advice to the original poster is, first, disambiguate by
making it Dim db As DAO.Database and if that gives an error, *then*
go to the references and add the DAO reference (and test whether you
can get away with completely removing the ADO reference, which is
useless for most Jet-only databases).

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 13 '05 #20
David W. Fenton wrote:
I think it's good practice to disambiguate regardless of which
library you're using.


How do you do this with variable representation?

For instance, collections are usually

dim frm as Access.Form, etc,

recordsets, etc

dim Dbs as DAO.Database

dim rst as ADODB.Recordset

dim rst as DAO.recordset

and so on.

But what about:

Dim strSilly as String
dim intI as Integer

etc?

--
Tim http://www.ucs.mun.ca/~tmarshal/
^o<
/#) "Burp-beep, burp-beep, burp-beep?" - Quaker Jake
/^^ "Whatcha doin?" - Ditto "TIM-MAY!!" - Me
Nov 13 '05 #21
Tim Marshall <TI****@PurplePandaChasers.Moertherium> wrote in
news:df**********@coranto.ucs.mun.ca:
David W. Fenton wrote:
I think it's good practice to disambiguate regardless of which
library you're using.
How do you do this with variable representation?


Eh? What do you mean?

Dim db As DAO.Database

is pretty easy.
For instance, collections are usually

dim frm as Access.Form, etc,
That doesn't need to be disambiguated, since there's only one
possible meaning for it, and because it's one that is *never* going
to be missing.
recordsets, etc

dim Dbs as DAO.Database

dim rst as ADODB.Recordset

dim rst as DAO.recordset
It's only ADO and DAO that need to be disambiguated.
and so on.

But what about:

Dim strSilly as String
dim intI as Integer

etc?


There is no possible library conflict there, since those are native
VBA data types, so there's nothing ambiguous about them, so no need
to qualify them.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 13 '05 #22
David W. Fenton wrote:
There is no possible library conflict there, since those are native
VBA data types, so there's nothing ambiguous about them, so no need
to qualify them.


Thanks. After reading Michka's article years ago, I went into a frenzy
of making sure everything I had was disambiuated, some of which, as
you've observed, unnecessarily. I just do it out of habit now and still
wonder from time to time about the VBA data types. 8)

--
Tim http://www.ucs.mun.ca/~tmarshal/
^o<
/#) "Burp-beep, burp-beep, burp-beep?" - Quaker Jake
/^^ "Whatcha doin?" - Ditto "TIM-MAY!!" - Me
Nov 13 '05 #23

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
by: Alberto Vera | last post by:
Hello: I have the next structure: How Can I make it using Python? How Can I update the value of 6?
8
by: Deepa | last post by:
I am writing a console app in c# wherein am converting a dataset into a CSV file. It works fine. But I have some values in the dataset which have a comma within(eg. A,B,C). When I view the CSV file...
5
by: Derek | last post by:
I came upon the idea of writting a logging class that uses a Python-ish syntax that's easy on the eyes (IMO): int x = 1; double y = 2.5; std::string z = "result"; debug = "Results:", x, y,...
3
by: SteelDetailer | last post by:
Thnaks in advance for considering this post. It's probably very simple, but..... I have an old VB6 application that allows me to create, save and edit a "project information file" that is a...
6
by: D | last post by:
How do you put a comma in a string that's passed as a function argument. The compiler thinks it's the end of that argument and I've tried the /, and "," but they don't seem to work.
4
by: G Patel | last post by:
Hi, I've read a book on C, and I understand how comma operators work, but my book didn't say that the comma operators between function arguments were not really comma operators (even though it...
3
by: Michael Yanowitz | last post by:
Hello: I am still relatively new to Python. I am confused by the syntax for tuples. I had: thread.start_new_thread(read_data_thread, (strDataFilename)) and got back the following error: ...
7
by: MattyWix | last post by:
Hi, How can I pass a semicolon or a comma as a macro argument. I wish to build an expression that in some cases has a comma - eg building a list of members for a structure, but in other...
15
by: Lighter | last post by:
In 5.3.3.4 of the standard, the standard provides that "The lvalue-to- rvalue(4.1), array-to-pointer(4.2),and function-to-pointer(4.3) standard conversions are not applied to the operand of...
6
by: lazukars | last post by:
Hi, I am attempting an ajax request. I am using PHP serverside. Below is what the response text should be. ajaxRequest.responseText ="{username_is_taken : "This username has been taken",}" ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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.