MS Access Concatenate功能 - 不允许重复

时间:2015-11-24 22:08:19

标签: sql vba ms-access

通过StackOverflow查看我能够找到我想要的东西:
Concatenating multiple rows into single line in MS Access [duplicate]
其中使用了以下功能:
Concatenate values from related records
但我无法弄清楚的是如何不从正在连接的行返回重复值。示例...我希望它返回给Customer1:

PartA, PartB, PartF

...不

PartA, PartA, PartA, PartB, PartB, PartF  

我不确定如何让这个自定义功能像这样工作。我的询问是......

SELECT DISTINCT PART_NUMBER, 
ConcatRelated("SUPPLIER_NAME","tbl_supplier_list",
              "PART_NUMBER=""" & [PART_NUMBER] & """")
FROM tbl_supplier_list;`

1 个答案:

答案 0 :(得分:2)

修改ConcatRelated()函数from Allen Browne,如下所示:

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
              If InStr(strOut, rsMV(0)) = 0 then  ''''' ADD THIS
                strOut = strOut & rsMV(0) & strSeparator
              End If                         ''''' ADD THIS
            End If
            rsMV.MoveNext
        Loop
        Set rsMV = Nothing
    ElseIf Not IsNull(rs(0)) Then
        strOut = strOut & rs(0) & strSeparator
    End If
    rs.MoveNext
Loop

InStr()函数将检查输出字符串strOut中是否已存在当前值。如果是(如返回0所示),If()语句将确保不添加当前值。