Access Merge Text from Different Rows

时间:2019-03-19 14:55:22

标签: ms-access text merge rows

I have a Access DB with following scheme:

+---------+---------+-----------+------------+-------------------------------------------+
| BrandNr |  TextNr |  LangCode |  OngoingNr |                    Text                   |
+---------+---------+-----------+------------+-------------------------------------------+
|       1 | AB      |         1 |          1 | Text beginns here but it doesn't end here |
|       1 | AB      |         1 |          2 | Text isn't finished so need second row    |
|       1 | AC      |         2 |          1 | New text                                  |
|       2 | hg2     |         1 |          1 | New brand new text                        |
+---------+---------+-----------+------------+-------------------------------------------+

Now I need to merge the text where BrandNR, TextNr and LangCode are the same. The Text should be ordered by the OngoingNr.

Some ideas?

1 个答案:

答案 0 :(得分:0)

我相信这将需要VBA。

一种可能的方法是调用一个VBA函数,该函数定义一个静态字符串类型变量,该变量针对BrandNRTextNrLangCode的每个新组合初始化为一个空字符串,其他与自身连接。

  • 使用 Alt + F11
  • 打开VBA IDE
  • 插入新的公共模块 Alt + I M
  • 将以下基本代码复制到新模块中:

    Function Concat(strInp As String, lngOrd As Long) As String
        Static strTmp As String
        If lngOrd = 1 Then strTmp = strInp Else strTmp = strTmp & " " & strInp
        Concat = strTmp
    End Function
    
  • 在MS Access中,使用以下SQL创建新查询,将MyTable更改为表名:

    select q.BrandNr, q.TextNr, q.LangCode, Concat(q.Text,q.OngoingNr) as Merged
    from
    (
        select t.* from MyTable t 
        order by t.BrandNr, t.TextNr, t.LangCode, t.OngoingNr
    ) q