SQL或VBA代码循环遍历两列数据 [英] SQL or VBA code to loop through two columns of data

查看:84
本文介绍了SQL或VBA代码循环遍历两列数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个196000行的表(产品代码和相关的产品代码)。产品包含一对多链接产品代码,我希望在一行中显示。列是:


ProductCode LinkProduct

000507 000123

000507 000456

000507 000789

000508 123456

000508 654321

000508 879456

000508 123789

等等


我想将此数据更改为:


ProductCode LinkProducts

000507 000123,000456,000789

000508 123456,654321,879456,123789




我花了好几天尝试在Access中解决这个问题但是可以''做到了。有人请帮我完成这项任务。

解决方案

如果您可以发布表的元数据(有关布局/结构的信息),这将更好用就像我在我的例子中使用的一样。单击回复按钮,您将可以访问我使用过的所有代码。 PK& FK代表Primary Key&外键分别为。请不要在格式化的布局中使用任何TAB,因为这将无法正确显示 - 仅用于布局的空间。
表名= [ tblStudent ]

展开 | 选择 | Wrap | 行号


字段名称数据类型

ProductCode文本

LinkProductCode文本


@limey


你仍然没有给我们muc h继续工作,所以我填写了一些缺少的空白:

  1. tblProductCodes包含Field [ProductCode],此字段是主键{TEXT}。
  2. tblLinkedProductCodes还包含[ProductCode]字段{TEXT},以及[LinkProductCode]字段。
  3. 基于[ProductCode] tblProductCodes和tblLinkedProductCodes之间存在1到MANY的关系]字段。
  4. 将以下公共功能复制并粘贴到标准代码模块:

    展开 | 选择 | 换行 | 行号

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.

解决方案

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


Field Name Data Type
ProductCode Text
LinkProductCode Text


@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


这篇关于SQL或VBA代码循环遍历两列数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆