473,474 Members | 1,762 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

SQL or VBA code to loop through two columns of data

3 New Member
I have a table with 196000 rows (product code and related product codes). A product has one to many link product codes that I wish to display in one row. The columns are:

ProductCode LinkProduct
000507 000123
000507 000456
000507 000789
000508 123456
000508 654321
000508 879456
000508 123789
etc etc

I want to change this data to:

ProductCode LinkProducts
000507 000123,000456,000789
000508 123456,654321,879456,123789
etc

I've spent days trying to work this out in Access but can't do it. Would somebody please help me with this task.
May 19 '09 #1
10 3788
NeoPa
32,556 Recognized Expert Moderator MVP
This will work much better if you can post the meta-data (info about the layout / structure) of the table in the same way as I use in my example. Click on the Reply button and you will have access to all the codes I've used. PK & FK stand for Primary Key & Foreign Key respectively. Please don't use any TABs in your formatted layout as this will not display correctly - Spaces only for layout.
Table Name=[tblStudent]
Expand|Select|Wrap|Line Numbers
  1. Field           Type        IndexInfo
  2. StudentID       AutoNumber  PK
  3. Family          String      FK
  4. SName           String
  5. University      String      FK
  6. Mark            Numeric
  7. LastAttendance  Date/Time
May 19 '09 #2
limey
3 New Member
Field Name Data Type
ProductCode Text
LinkProductCode Text
May 19 '09 #3
ADezii
8,834 Recognized Expert Expert
@limey
You still did not give us much to work on, so I filled in a few missing blanks:
  1. tblProductCodes contains the Field [ProductCode] and this Field is the Primary Key {TEXT}.
  2. tblLinkedProductCodes also contains the [ProductCode] Field {TEXT}, as well as a [LinkProductCode] Field.
  3. There is a 1 to MANY Relationship between tblProductCodes and tblLinkedProductCodes based on the [ProductCode] Field.
  4. Copy-and-Paste the following 'Public' Function to a Standard Code Module:
    Expand|Select|Wrap|Line Numbers
    1. Public Function fGenerateLinkProductCodes(strProductCode As String) As String
    2. Dim MyDB As DAO.Database
    3. Dim rstProd As DAO.Recordset
    4. Dim strSQL As String
    5. Dim strBuild As String
    6.  
    7. Set MyDB = CurrentDb
    8.  
    9. strSQL = "Select tblLinkedProductCodes.[LinkProductCode] From tblLinkedProductCodes Where " & _
    10.          "tblLinkedProductCodes.[ProductCode] = '" & strProductCode & "';"
    11.  
    12. Set rstProd = MyDB.OpenRecordset(strSQL, dbOpenForwardOnly)
    13.  
    14. With rstProd
    15.   If Not .BOF And Not .EOF Then     'Related Records exist
    16.     Do While Not .EOF
    17.       strBuild = strBuild & ![LinkProductCode] & ", "
    18.         .MoveNext
    19.     Loop
    20.       fGenerateLinkProductCodes = Left$(strBuild, Len(strBuild) - 2)
    21.   Else
    22.     fGenerateLinkProductCodes = "No Related Records for " & strProductCode
    23.   End If
    24. End With
    25.  
    26. rstProd.Close
    27. Set rstProd = Nothing
  5. Create the following Query:
    Expand|Select|Wrap|Line Numbers
    1. SELECT tblProductCodes.ProductCode, fGenerateLinkProductCodes([ProductCode]) AS LinkProducts
    2. FROM tblProductCodes;
  6. Query Results:
    Expand|Select|Wrap|Line Numbers
    1. ProductCode    LinkProducts
    2. 000507         000123, 000456, 000789
    3. 000508         123456, 654321, 879456, 123789
    4. 000839         No Related Records for 000839
  7. This is a code-based approach utilizing a Public Function in a Query. There is probably a more efficient SQL based solution which one of the other guys will come up with, so stay tuned.
  8. Any questions, feel free to ask.
May 19 '09 #4
limey
3 New Member
Thank you very much ADezii. Your solution is exactly what I was hoping for and I can use this code as a starting point for doing similar things with Access data in the future.

Have a great day and once again thank you for your help.
May 20 '09 #5
NeoPa
32,556 Recognized Expert Moderator MVP
This isn't remotely straightforward Limey. The main reason for this is it goes against the concepts of a relational database.

Furthermore (and probably for this very reason) I don't believe there is a simple SQL solution.

That's a long way from saying we haven't had similar requests before mind you (Sorry. My earlier confusion was not the question, but simply my overlooking it could be as simple as you stated it).

A solution I prepared earlier (probably similar in many respects to ADezii's) is Producing a List from Multiple Records.

NB. There is a limit of 255 chars that a string field (not variable in VBA - just a string field in a recordset) can take.
May 20 '09 #6
ADezii
8,834 Recognized Expert Expert
@limey
You are quite welcome.
May 20 '09 #7
ADezii
8,834 Recognized Expert Expert
@NeoPa
Just for curiosity, NeoPa. No data or structural changes are being made, Referential Integrity is not being violated, and the Database itself is not being altered in any manner. All that is happening is that data is being displayed in a Read Only Query for observation purposes. I honestly do not see this as a violation of the concepts of a Relational Database. Just my simple point of view.
May 20 '09 #8
ChipR
1,287 Recognized Expert Top Contributor
The OP said "I want to change this data to:" and since 196,000 rows were mentioned, I believe the intent is to store the data this way to reduce the number of rows. I would caution against that, since once you do this you can't actually use the data.
May 20 '09 #9
ADezii
8,834 Recognized Expert Expert
Sorry ChipR and NeoPa, but I must have totally missed that. In that case, you of course, are 100% correct.
May 20 '09 #10
NeoPa
32,556 Recognized Expert Moderator MVP
@ADezii
I find myself struggling mightily to express why I feel this is an issue, at least for the purist.

The words don't come easily, and if I'm honest, I don't think a discussion of the nitty-gritty of this would prove very helpful. I certainly couldn't put forward a compelling argument.

Agree to disagree seems fine :)

PS. Absolutely no need to apologise. Your point stands alone, regardless of what the OP in this instance requires.
May 20 '09 #11

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

Similar topics

4
by: Shufen | last post by:
Hi, I'm a newbie that just started to learn python, html and etc. I have some questions to ask and hope that someone can help me on. I'm trying to code a python script (with HTML) to get...
2
by: Brent | last post by:
Hello all.. I have a recordset that has 12 records in it, and about 25 columns. Unfortunately, I have to write the records out as columns in an ASP page, and the columns as rows. So, what I have done...
20
by: Steve Jorgensen | last post by:
Hi all, I've just finished almost all of what has turned out to be a real bear of a project. It has to import data from a monthly spreadsheet export from another program, and convert that into...
2
by: Guadala Harry | last post by:
I have some logic that currently processes values retrieved by looping through a HashTable/ListDictionary structure (a hash table that contains ListDictionary objects as its values). Logically it's...
1
by: jimb | last post by:
I can get the dropdownlist into the datagrid, and I can populate it, but I can't read it. Anybody have a working example of a dropdownlist in an editable grid? Thanks. -- .....
2
by: Ronald S. Cook | last post by:
In the code below, I get a runtime error where indicated. The error is: "Column 'TICKETNO' does not belong to table TICKET_DEDUCTIONS." If I put a breakpoint on that line (so not yet executed)...
3
by: shapper | last post by:
Hello, I need to loop though each row in a GridView and if the checkbox is a Template Field is checked I want to display the value of an invisible column named "LevelName". I tried everything...
9
by: TF | last post by:
Hello all, I made a ASP.NET 2.0 site that shows possible "recipes" for paint colors stored in an access dbase. Basically, 1000 colors are stored with specific RGB values in separate columns. A...
8
by: cberthu | last post by:
I all I am trying to go into a loop to insert data into a new table, the loop is required because the select genearte to much data and the transaction log gets full. So i found a way to get this...
3
by: xmail123 | last post by:
Why does this code work? I am new to C# and have been studying this piece of code. It loops through an Adjacency Matrix table to populate a tree view. I have two questions about why this code...
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:
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
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
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...
1
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...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.