473,508 Members | 2,477 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Producing a List from Multiple Records

NeoPa
32,557 Recognized Expert Moderator MVP
A number of posters have asked to be shown how to produce a list of items from multiple records which are (potentially) grouped together.
Take the following data for instance (from a table called [tblForum]) :
Expand|Select|Wrap|Line Numbers
  1. Zone             Forum
  2. Community        Introductions
  3. Community        Community Cafe
  4. Community        Software Development
  5. Community        Jobs / Contract Work
  6. Community        Experts Panel
  7. Programming      C++ / C
  8. Programming      Java
  9. Programming      .NET
  10. Programming      Visual Basic
  11. Programming      Python
  12. Web Development  PHP
  13. Web Development  Ruby / Rails
  14. Web Development  Perl
  15. Web Development  ASP
  16. Web Development  ColdFusion
  17. Web Development  Javascript / Ajax
  18. Web Development  Flash
  19. Web Development  XML
  20. Web Development  WAP / WML
  21. Web Development  HTML / CSS
  22. Database Help    MySQL
  23. Database Help    Oracle
  24. Database Help    SQL Server
  25. Database Help    PostgreSQL
  26. Database Help    Access
  27. Database Help    DB2
  28. Sys Admin & OS   Apache
  29. Sys Admin & OS   IIS
  30. Sys Admin & OS   Networking
  31. Sys Admin & OS   Linux / Unix / BSD
  32. Sys Admin & OS   Windows
If you wanted this to be grouped together into lists (as below) then read on.
Expand|Select|Wrap|Line Numbers
  1. Zone             Forums
  2. Community        Introductions, Community Cafe, Software Development, Jobs / Contract Work, Experts Panel
  3. Programming      C++ / C, Java, .NET, Visual Basic, Python
  4. Web Development  PHP, Ruby / Rails, Perl, ASP, ColdFusion, Javascript / Ajax, Flash, XML, WAP / WML, HTML / CSS
  5. Database Help    MySQL, Oracle, SQL Server, PostgreSQL, Access, DB2
  6. Sys Admin & OS   Apache, IIS, Networking, Linux / Unix / BSD, Windows
Firstly, create a function in a standard module similar to the example code attached :
Expand|Select|Wrap|Line Numbers
  1. 'Concat Returns lists of items which are within a grouped field
  2. Public Function Concat(strGroup As String, _
  3.                        strItem As String) As String
  4.     Static strLastGroup As String
  5.     Static strItems As String
  6.  
  7.     If strGroup = strLastGroup Then
  8.         strItems = strItems & ", " & strItem
  9.     Else
  10.         strLastGroup = strGroup
  11.         strItems = strItem
  12.     End If
  13.     Concat = strItems
  14. End Function
The SQL for calling this function would be of the form :
Expand|Select|Wrap|Line Numbers
  1. SELECT   [Zone],
  2.          Max(Concat([Zone], [Forum])) AS [Forums]
  3. FROM     [tblForum]
  4. GROUP BY [Zone]
Another form of this (to drop duplicates) would be :
Expand|Select|Wrap|Line Numbers
  1. 'Concat Returns lists of items which are within a grouped field
  2. Public Function Concat(strGroup As String, _
  3.                        strItem As String) As String
  4.     Static strLastGroup As String
  5.     Static strItems As String
  6.  
  7.     If strGroup = strLastGroup Then
  8.         If InStr(", " & strItems & ", ", ", " & strItem & ", ") = 0 Then _
  9.             strItems = strItems & ", " & strItem
  10.     Else
  11.         strLastGroup = strGroup
  12.         strItems = strItem
  13.     End If
  14.     Concat = strItems
  15. End Function
Apr 30 '07 #1
1 8198
Mariostg
332 Contributor
This is nice.

I combined the two functions by adding a third parameter "uniq"
Expand|Select|Wrap|Line Numbers
  1. 'Concat Returns lists of items which are within a grouped field
  2. 'Set uniq=-1 (True) to return only unique values.
  3. Public Function ConcatLine(strGroup As String, _
  4.                        strItem As String, uniq As Boolean) As String
  5.     Static strLastGroup As String
  6.     Static strItems As String
  7.     Static i As Integer
  8.  
  9.     If strGroup = strLastGroup Then
  10.         If uniq Then
  11.             If InStr(", " & strItems & ", ", ", " & strItem & ", ") = 0 Then
  12.                 strItems = strItems & ", " & strItem
  13.             End If
  14.         Else
  15.             strItems = strItems & ", " & strItem
  16.         End If
  17.     Else
  18.         strLastGroup = strGroup
  19.         strItems = strItem
  20.     End If
  21.     ConcatLine = strItems
  22.     i = i + 1
  23.     Debug.Print i & " "; Len(ConcatLine) & " " & ConcatLine
  24. End Function
Jan 12 '12 #2

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

Similar topics

3
18192
by: arthur-e | last post by:
I can filter a query for a report on a field of a subform BUT now.... I'd like to be able to select more than one item in a multi-list box to select all the records. ALSO to use two fields (or...
1
2033
by: Brandon Schultz | last post by:
I have created a list box so that I can navigate through records. The AfterUpdate event looks like this; Me.RecordsetClone.FindFirst " = '" & Me! & "'" Me.Bookmark = Me.RecordsetClone.Bookmark ...
7
2413
by: Jed Parsons | last post by:
Hi, I'm using the logging module for the first time. I'm using it from within Zope Extensions. My problem is that, for every event logged, the logger is producing multiple identical entries...
7
2229
by: beginner | last post by:
Hi Everyone, I have a simple list reconstruction problem, but I don't really know how to do it. I have a list that looks like this: l= What I want to do is to reorganize it in groups,...
5
2828
by: Grubsy4u | last post by:
Hi all, I have recently created a list box in a form.....I wanted to know if there was a way of searching for multiple records within the list box. How can i get this list box to search for...
13
1494
by: Joel Koltner | last post by:
Is there an easy way to get a list comprehension to produce a flat list of, say, for each input argument? E.g., I'd like to do something like: for x in range(4) ] ....and receive
1
4866
by: KrazyKasper | last post by:
Access 2003 – Multi-Column List Box – Select Multiple Items I have a multi-column (3 columns) list box that works well to select one set of records or all sets of records (based on the first field...
1
6767
by: woodey2002 | last post by:
Hi Everyone and many thanks for your time.. I am trying to begin access and a bit of VBA i am enjoying it but I have a annoying problem I just can’t get any where on. My databse mostly includes...
0
7231
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
7133
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
7336
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
7405
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
5643
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3214
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3198
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
773
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
435
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.