Can a SQL script be written to sequentially go through all the records in a table and, based on certian criteria, copy text data from a specific field and concatinate the text, in the order found, into a field of one record in another table?
27 Replies
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
I'm not sure how to respond to you. All I know is that the code as written, and provided to be by a knowledgeable VBA programmer, does work properly and does update my table the way I wanted it to.
I changed the data to include a record with a single quote, as shown below, but there were no changes in the results. And then I searched the tables again and found many example of " ' " in the name fields (O'Connor, O'Heron, etc), also with no errors.
Maybe there are two ways to arrive at the same result?
Don
ImmediateView
UPDATE tblCensus4 SET tblCensus4.[Desc] = tblCensus4.[Desc] & Chr(13) & Chr(10) & "Ida Louise Wagner" WHERE tblCensus4.[IDER]=168 UPDATE tblCensus4 SET tblCensus4.[Desc] = tblCensus4.[Desc] & Chr(13) & Chr(10) & "June Barbara Quigley" WHERE tblCensus4.[IDER]=168 UPDATE tblCensus4 SET tblCensus4.[Desc] = tblCensus4.[Desc] & Chr(13) & Chr(10) & "Ida St' LouiseWagner" WHERE tblCensus4.[IDER]=179 UPDATE tblCensus4 SET tblCensus4.[Desc] = tblCensus4.[Desc] & Chr(13) & Chr(10) & "Francis Albert (Frank) Quigley" WHERE tblCensus4.[IDER]=179 UPDATE tblCensus4 SET tblCensus4.[Desc] = tblCensus4.[Desc] & Chr(13) & Chr(10) & "June Barbara Quigley" WHERE tblCensus4.[IDER]=179
Results in tblCensus4.[Desc]
Household Members: Francis Eugene (Frank) Quigley Ida St' Louise Wagner Francis Albert (Frank) Quigley Ida St' Louise Wagner Francis Albert (Frank) Quigley June Barbara Quigley
Jefferson - I am not an experienced VBA programmer. All I know is that the code works. Here are the first several passes though the recordset from Debug.Print SQL; Var:
UPDATE tblCensus4 SET tblCensus4.[Desc] = tblCensus4.[Desc] & Chr(13) & Chr(10) & "Ida Louise Wagner" WHERE tblCensus4.[IDER]=168
UPDATE tblCensus4 SET tblCensus4.[Desc] = tblCensus4.[Desc] & Chr(13) & Chr(10) & "June Barbara Quigley" WHERE tblCensus4.[IDER]=168 UPDATE tblCensus4 SET tblCensus4.[Desc] = tblCensus4.[Desc] & Chr(13) & Chr(10) & "Ida Louise Wagner" WHERE tblCensus4.[IDER]=179 UPDATE tblCensus4 SET tblCensus4.[Desc] = tblCensus4.[Desc] & Chr(13) & Chr(10) & "Francis Albert (Frank) Quigley" WHERE tblCensus4.[IDER]=179 UPDATE tblCensus4 SET tblCensus4.[Desc] = tblCensus4.[Desc] & Chr(13) & Chr(10) & "June Barbara Quigley" WHERE tblCensus4.[IDER]=179 UPDATE tblCensus4 SET tblCensus4.[Desc] = tblCensus4.[Desc] & Chr(13) & Chr(10) & "Robert Weston (Bob) Quigley" WHERE tblCensus4.[IDER]=179 UPDATE tblCensus4 SET tblCensus4.[Desc] = tblCensus4.[Desc] & Chr(13) & Chr(10) & "Theresa Margaret Anderson" WHERE tblCensus4.[IDER]=393
Jefferson - That line of code gives me a syntax error, with or w/o a period between rs and [IDER]. The column name is IDER in tblCensus4 and it is an Autonummuber data type. In tblCensus1Sorted the column name is IDER and it is an Number data type
IDER refers to a common field name in the two tables. The problem is that the first table has multiple records with the same IDER value and the second table has only one record for that IDER value. I'm trying to concatenate fields from the first table for each IDER value into the second one for that IDER value.
The attached code will get all the right values from the first table, but it throws them all into every record in the second table; ie, with no regard to the IDER values.
Here is my problem. I need to concatenate two fields from multiple records into a single field, separated by CR&LF, and copy them into one field of one record in a related table (common "IDER"). I thought this would do it, but I keep getting this text box:
Donald QuigleyPosted May 19, 2020, 6:02 PM
numvalue = rs.IDER.Value
:
SQL = "UPDATE tblCensus4 SET tblCensus4.[Desc] = tblCensus4.[Desc] & Chr(13) & Chr(10) & """ & strvalue & """ WHERE tblCensus4.[IDER]=" & numvalue
Jefferson S. MottaPosted May 19, 2020, 10:59 AM
Jefferson S. MottaPosted May 19, 2020, 10:58 AM
Donald QuigleyPosted May 19, 2020, 6:38 AM
Jefferson S. MottaPosted May 18, 2020, 3:28 PM
Donald QuigleyPosted May 16, 2020, 3:38 PM
UPDATE tblCensus4 SET tblCensus4.[Desc] = tblCensus4.[Desc] & Chr(13) & Chr(10) & "June Barbara Quigley" WHERE tblCensus4.[IDER]=168
UPDATE tblCensus4 SET tblCensus4.[Desc] = tblCensus4.[Desc] & Chr(13) & Chr(10) & "Ida St' Louise Wagner" WHERE tblCensus4.[IDER]=179
UPDATE tblCensus4 SET tblCensus4.[Desc] = tblCensus4.[Desc] & Chr(13) & Chr(10) & "Francis Albert (Frank) Quigley" WHERE tblCensus4.[IDER]=179
UPDATE tblCensus4 SET tblCensus4.[Desc] = tblCensus4.[Desc] & Chr(13) & Chr(10) & "June Barbara Quigley" WHERE tblCensus4.[IDER]=179
Francis Eugene (Frank) Quigley
Ida St' Louise Wagner
Francis Albert (Frank) Quigley
Ida St' Louise Wagner
Francis Albert (Frank) Quigley
June Barbara Quigley
Jefferson S. MottaPosted May 16, 2020, 1:00 PM
Donald QuigleyPosted May 15, 2020, 4:26 PM
UPDATE tblCensus4 SET tblCensus4.[Desc] = tblCensus4.[Desc] & Chr(13) & Chr(10) & "Ida Louise Wagner" WHERE tblCensus4.[IDER]=179
UPDATE tblCensus4 SET tblCensus4.[Desc] = tblCensus4.[Desc] & Chr(13) & Chr(10) & "Francis Albert (Frank) Quigley" WHERE tblCensus4.[IDER]=179
UPDATE tblCensus4 SET tblCensus4.[Desc] = tblCensus4.[Desc] & Chr(13) & Chr(10) & "June Barbara Quigley" WHERE tblCensus4.[IDER]=179
UPDATE tblCensus4 SET tblCensus4.[Desc] = tblCensus4.[Desc] & Chr(13) & Chr(10) & "Robert Weston (Bob) Quigley" WHERE tblCensus4.[IDER]=179
UPDATE tblCensus4 SET tblCensus4.[Desc] = tblCensus4.[Desc] & Chr(13) & Chr(10) & "Theresa Margaret Anderson" WHERE tblCensus4.[IDER]=393
Jefferson S. MottaPosted May 15, 2020, 11:25 AM
Donald QuigleyPosted May 15, 2020, 11:04 AM
numvalue = rs.IDER.Value
:
SQL = "UPDATE tblCensus4 SET tblCensus4.[Desc] = tblCensus4.[Desc] & Chr(13) & Chr(10) & """ & strvalue & """ WHERE tblCensus4.[IDER]=" & numvalue
I appreciate you taking the time to help me as well.
Jefferson S. MottaPosted May 15, 2020, 10:43 AM
Donald QuigleyPosted May 15, 2020, 10:23 AM
Jefferson S. MottaPosted May 15, 2020, 9:34 AM