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: - Name | Number
-
--------------------------
-
Charlie | 1
-
Charlie | 2
-
James | 3
-
James | 4
-
Michelle | 5
-
Michelle | 6
TABLE 2: - Name | Country
-
------------------------------
-
Charlie | Brazil
-
Charlie | France
-
James | Japan
-
Michelle | USA
TABLE 3 - TABLES that totalizes the concatenation: - Name | Number | Country
-
----------------------------------------------
-
Charlie | 1,2 | Brazil,France
-
James | 3,4 | Japan
-
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: - Option Explicit
-
-
Public Function ConcatRelated(strField As String, _
-
strTable As String, _
-
Optional strWhere As String, _
-
Optional strOrderBy As String, _
-
Optional strSeparator = ", ") As Variant
-
On Error GoTo Err_Handler
-
'Purpose: Generate a concatenated string of related records.
-
'Return: String variant, or Null if no matches.
-
'Arguments: strField = name of field to get results from and concatenate.
-
' strTable = name of a table or query.
-
' strWhere = WHERE clause to choose the right values.
-
' strOrderBy = ORDER BY clause, for sorting the values.
-
' strSeparator = characters to use between the concatenated values.
-
'Notes: 1. Use square brackets around field/table names with spaces or odd characters.
-
' 2. strField can be a Multi-valued field (A2007 and later), but strOrderBy cannot.
-
' 3. Nulls are omitted, zero-length strings (ZLSs) are returned as ZLSs.
-
' 4. Returning more than 255 characters to a recordset triggers this Access bug:
-
' http://allenbrowne.com/bug-16.html
-
Dim rs As DAO.Recordset 'Related records
-
Dim rsMV As DAO.Recordset 'Multi-valued field recordset
-
Dim strSql As String 'SQL statement
-
Dim strOut As String 'Output string to concatenate to.
-
Dim lngLen As Long 'Length of string.
-
Dim bIsMultiValue As Boolean 'Flag if strField is a multi-valued field.
-
-
'Initialize to Null
-
ConcatRelated = Null
-
-
'Build SQL string, and get the records.
-
strSql = "SELECT " & strField & " FROM " & strTable
-
If strWhere <> vbNullString Then
-
strSql = strSql & " WHERE " & strWhere
-
End If
-
If strOrderBy <> vbNullString Then
-
strSql = strSql & " ORDER BY " & strOrderBy
-
End If
-
Set rs = DBEngine(0)(0).OpenRecordset(strSql, dbOpenDynaset)
-
'Determine if the requested field is multi-valued (Type is above 100.)
-
bIsMultiValue = (rs(0).Type > 100)
-
-
'Loop through the matching records
-
Do While Not rs.EOF
-
If bIsMultiValue Then
-
'For multi-valued field, loop through the values
-
Set rsMV = rs(0).Value
-
Do While Not rsMV.EOF
-
If Not IsNull(rsMV(0)) Then
-
strOut = strOut & rsMV(0) & strSeparator
-
End If
-
rsMV.MoveNext
-
Loop
-
Set rsMV = Nothing
-
ElseIf Not IsNull(rs(0)) Then
-
strOut = strOut & rs(0) & strSeparator
-
End If
-
rs.MoveNext
-
Loop
-
rs.Close
-
-
'Return the string without the trailing separator.
-
lngLen = Len(strOut) - Len(strSeparator)
-
If lngLen > 0 Then
-
ConcatRelated = Left(strOut, lngLen)
-
End If
-
-
Exit_Handler:
-
'Clean up
-
Set rsMV = Nothing
-
Set rs = Nothing
-
Exit Function
-
-
Err_Handler:
-
MsgBox "Error " & Err.Number & ": " & Err.Description, vbExclamation, "ConcatRelated()"
-
Resume Exit_Handler
-
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?
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. (^_^)
I did the changes. Sorry for the unexperiencie!
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 Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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?
|
by: NeoPa |
last post by:
**********
|
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
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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: 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...
|
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,...
| |