473,396 Members | 2,011 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,396 software developers and data experts.

Concatenating values to a memo field returns few results

I've got the following problem with M$ Access:

I have many tables, all of them with only 2 columns (one of the columns works as a PK to identify the rows - In this example, it will be the column "NAME") and i need to concatenate every value from every column into one field in another table, something like:

TABLE 1:
Expand|Select|Wrap|Line Numbers
  1.         Name      |    Number
  2.     --------------------------
  3.         Charlie   |      1
  4.         Charlie   |      2
  5.         James     |      3
  6.         James     |      4
  7.         Michelle  |      5
  8.         Michelle  |      6
TABLE 2:
Expand|Select|Wrap|Line Numbers
  1.         Name      |      Country
  2.     ------------------------------
  3.         Charlie   |      Brazil
  4.         Charlie   |      France
  5.         James     |      Japan
  6.         Michelle  |       USA
TABLE 3 - TABLES that totalizes the concatenation:
Expand|Select|Wrap|Line Numbers
  1.         Name      |    Number   |     Country
  2.     ----------------------------------------------
  3.         Charlie   |     1,2     |  Brazil,France
  4.         James     |     3,4     |      Japan
  5.         Michelle  |     5,6     |       USA
Until now, I was able to create an append that uses a function in VBA to concatenate these values to the fields in Table 3. The function is avaiable in: http://allenbrowne.com/func-concat.html, as below:

Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2.  
  3. Public Function ConcatRelated(strField As String, _
  4.     strTable As String, _
  5.     Optional strWhere As String, _
  6.     Optional strOrderBy As String, _
  7.     Optional strSeparator = ", ") As Variant
  8. On Error GoTo Err_Handler
  9.     'Purpose:   Generate a concatenated string of related records.
  10.     'Return:    String variant, or Null if no matches.
  11.     'Arguments: strField = name of field to get results from and concatenate.
  12.     '           strTable = name of a table or query.
  13.     '           strWhere = WHERE clause to choose the right values.
  14.     '           strOrderBy = ORDER BY clause, for sorting the values.
  15.     '           strSeparator = characters to use between the concatenated values.
  16.     'Notes:     1. Use square brackets around field/table names with spaces or odd characters.
  17.     '           2. strField can be a Multi-valued field (A2007 and later), but strOrderBy cannot.
  18.     '           3. Nulls are omitted, zero-length strings (ZLSs) are returned as ZLSs.
  19.     '           4. Returning more than 255 characters to a recordset triggers this Access bug:
  20.     '               http://allenbrowne.com/bug-16.html
  21.     Dim rs As DAO.Recordset         'Related records
  22.     Dim rsMV As DAO.Recordset       'Multi-valued field recordset
  23.     Dim strSql As String            'SQL statement
  24.     Dim strOut As String            'Output string to concatenate to.
  25.     Dim lngLen As Long              'Length of string.
  26.     Dim bIsMultiValue As Boolean    'Flag if strField is a multi-valued field.
  27.  
  28.     'Initialize to Null
  29.     ConcatRelated = Null
  30.  
  31.     'Build SQL string, and get the records.
  32.     strSql = "SELECT " & strField & " FROM " & strTable
  33.     If strWhere <> vbNullString Then
  34.         strSql = strSql & " WHERE " & strWhere
  35.     End If
  36.     If strOrderBy <> vbNullString Then
  37.         strSql = strSql & " ORDER BY " & strOrderBy
  38.     End If
  39.     Set rs = DBEngine(0)(0).OpenRecordset(strSql, dbOpenDynaset)
  40.     'Determine if the requested field is multi-valued (Type is above 100.)
  41.     bIsMultiValue = (rs(0).Type > 100)
  42.  
  43.     'Loop through the matching records
  44.     Do While Not rs.EOF
  45.         If bIsMultiValue Then
  46.             'For multi-valued field, loop through the values
  47.             Set rsMV = rs(0).Value
  48.             Do While Not rsMV.EOF
  49.                 If Not IsNull(rsMV(0)) Then
  50.                     strOut = strOut & rsMV(0) & strSeparator
  51.                 End If
  52.                 rsMV.MoveNext
  53.             Loop
  54.             Set rsMV = Nothing
  55.         ElseIf Not IsNull(rs(0)) Then
  56.             strOut = strOut & rs(0) & strSeparator
  57.         End If
  58.         rs.MoveNext
  59.     Loop
  60.     rs.Close
  61.  
  62.     'Return the string without the trailing separator.
  63.     lngLen = Len(strOut) - Len(strSeparator)
  64.     If lngLen > 0 Then
  65.         ConcatRelated = Left(strOut, lngLen)
  66.     End If
  67.  
  68. Exit_Handler:
  69.     'Clean up
  70.     Set rsMV = Nothing
  71.     Set rs = Nothing
  72.     Exit Function
  73.  
  74. Err_Handler:
  75.     MsgBox "Error " & Err.Number & ": " & Err.Description, vbExclamation, "ConcatRelated()"
  76.     Resume Exit_Handler
  77. End Function


The problem is: Even using a Memo field in Table 1, Table 2 and Table 3, my concatenation doesn't let me concatenate too many values, and I dont get every value needed into table 3.

Can someone please help me?
Jan 6 '14 #1
3 1393
zmbd
5,501 Expert Mod 4TB
Would you please do the basic troubleshooting as outlined here: > Before Posting (VBA or SQL) Code

After you can compile everything without error, then it would help if you would show us your work to date:
Just open your code,
Copy
Paste here in a post
Select the inserted text
Click on the [CODE/] in the toolbar to format.

What we find is that there are often typos.

Please no attachments at this point in time. (^_^)
Jan 6 '14 #2
I did the changes. Sorry for the unexperiencie!
Jan 6 '14 #3
zmbd
5,501 Expert Mod 4TB
No need to applogize. (^_^)

Do you have everything working now?

one of the columns works as a PK to identify the rows - In this example, it will be the column "NAME"
Yet in the example data you have two records where the field [name]="Michelle" so this field can not be a primary key.

Another thing I should have pointed out to you is to not use reserved words as field names:
Access 2007 reserved words and symbols
AllenBrowne- Problem names and reserved words in Access
Jan 6 '14 #4

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

Similar topics

2
by: Jim Plante | last post by:
I have a memo field in a MS Access table. The contents of the field may look like this: 1. This is number one 2. This is number 2 3. Skipping lines is fun if your #3 But when I write it...
3
by: monika | last post by:
hi .. i am using memo field in a field which has to take long data. when i do the insert its now givng me error: Error Type: Microsoft OLE DB Provider for ODBC Drivers (0x80040E14) Syntax...
3
by: Helgardh | last post by:
I have a linked table (Access 2003) to an Outlook inbox. The body of the e-mail messages are in a memo field. My problem is that I need to "read" the memo and find data on certain lines. The...
6
by: Matt | last post by:
I'm having difficulty with trying to update a Access memo field through an SQL statement where the value I'm trying to pass is longer than 255 characters. The field is being truncated. I'm using...
7
by: midlothian | last post by:
Does using CStr on a memo field truncate it to 255 characters? Seems like this is happening with some data in my tables. Is there a way around this?
0
NeoPa
by: NeoPa | last post by:
**********
4
by: John | last post by:
Is it possible to center a memo field value vertically? I use a memo field for printing address labels and I would like the addresses to show up centered vertically. Thanks, john
1
thewesties
by: thewesties | last post by:
Could somebody please help me before I dump a gallon of water on my pc! I am very happy to have come across this site on the internet. TheScripts has helped me through so many issues to this...
2
by: bennylynch | last post by:
Hi, I am attempting to fill in 2 memo fields in the same form, which is fine, however the information on the second memo field is based on the information on the first one, so I need to hold the...
8
by: pteare | last post by:
Hello, I was wondering if someone might be able to help me. I’m a little stuck as to how to put the results of a query into a memo field. I run a ski chalet company and I have a bookings...
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:
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.