Wrox Programmer Forums
|
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 14th, 2008, 10:33 AM
Authorized User
 
Join Date: Sep 2007
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Default concatenating records

Hi,

I have the below data.

Col1 Col2 Col3 Col4 Col5
------------------------------------------------
CompA Sales Dept UK
CompA Sales Dept USA
CompA Sales Dept Asia
CompB HR Dept USA
CompB HR Dept UK
------------------------------------------------

I am trying to concatenate the above records to get the below result.
Can I use the concatenation function in a query somehow ? Can somebody please point me in the right direction.

------------------------------------------------
CompA Sales Dept USA UK Asia
CompB HR Dept USA, UK
------------------------------------------------

Thanks
 
Old October 15th, 2008, 07:59 AM
Authorized User
 
Join Date: Sep 2007
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Can somebody please guide me on how to do it.

Thanks

 
Old October 15th, 2008, 02:15 PM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

Wow, this shows up a lot. This is the result of improper table / relationship design. I have done this with code if you are interested in that solution.

mmcdonal

Look it up at: http://wrox.books24x7.com
 
Old October 15th, 2008, 03:45 PM
Authorized User
 
Join Date: Sep 2007
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

This data is not directly from a table. This is a query result from various tables. And I am not able to get it in the format I want (basically one row for one company).

Is there a way of querying to get it directly in the required format.

Thanks

 
Old October 16th, 2008, 06:30 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

You will need to post the table structure. Show some sample data in each table.


mmcdonal

Look it up at: http://wrox.books24x7.com
 
Old October 16th, 2008, 02:23 PM
Authorized User
 
Join Date: Sep 2007
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi mmcdonal,

Is there a function to concatenate data from all fields without having to specify the actual field name ?

Thanks

 
Old October 16th, 2008, 02:25 PM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

You can write code to refer to columns in recordsets as Column 0, Column 1, etc. It is sometimes easier to use actual column names. But without knowing what the underlying data structure is, there is no way to write code or queries to concatenate this the way you want.

mmcdonal

Look it up at: http://wrox.books24x7.com
 
Old November 5th, 2008, 12:43 PM
pjm pjm is offline
Authorized User
 
Join Date: Jul 2006
Posts: 70
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Here's some code I sometimes use to work with tables:

Dim db As Database
Dim rs As Recordset

Set db = CurrentDb()

With db.TableDefs(SourceTable)
    For i = 0 To .Fields.Count - 1
        fieldName(i) = .Fields(i).Name
    Next i
End With

You can then reference the fields like this:

      rs(fieldName(i))

      -Phil-
 
Old November 5th, 2008, 05:52 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Pardon me, but that's silly.

There will be *NO DIFFERENCE* between
    rs(fieldName(i))
and simply (and faster)
    rs(i)

What's the point?
 
Old November 6th, 2008, 05:07 PM
pjm pjm is offline
Authorized User
 
Join Date: Jul 2006
Posts: 70
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Certainly I am suitably chastised for my silliness. Obviously there can be no reason for having a table's field names stored in an array.


      -Phil-





Similar Threads
Thread Thread Starter Forum Replies Last Post
Concatenating like records pvasudevan SQL Server 2000 35 August 14th, 2008 09:28 AM
Concatenating strings of code sassora Word VBA 1 March 31st, 2006 02:39 PM
Concatenating many XML files srivalli9 XML 4 March 30th, 2006 03:05 AM
Concatenating column values arnabghosh Access 1 September 21st, 2005 06:45 AM
concatenating arrays allee_man BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 0 August 19th, 2005 08:55 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.