Concatenate fields from one column in one table into a single, comma delimited value in another table

前端 未结 2 1926
無奈伤痛
無奈伤痛 2020-12-04 00:00

Any help that can be provided to a Access and VB noob would be greatly appreciated. What I\'m trying to do is concatenate the values from one table and insert it as a comma

2条回答
  •  半阙折子戏
    2020-12-04 00:24

    You can use Concatenate values from related records by Allen Browne for this. Copy the function code from that web page and paste it into a new standard module. Save the module and give the module a name different from the function name; modConcatRelated would work.

    Then I think you should be able to use the function in a query even though you're not proficient with VBA.

    First notice I changed the field names in TableA to replace spaces with underscores. With that change, this query ...

    SELECT
        sub.Operating_System, 
        ConcatRelated("Machine_Name", "TableA", 
            "Operating_System = '" & sub.Operating_System & "'") AS Machines
    FROM [SELECT DISTINCT Operating_System FROM TableA]. AS sub;
    

    ... produces this result set:

    Operating_System Machines
    Linux            Server01, Server02
    Solaris          Server05
    Windows          Server03, Server04
    

    If you can't rename the fields as I did, use a separate query to select the distinct operating systems.

    SELECT DISTINCT TableA.[Operating System]
    FROM TableA;
    

    Save that as qryDistinctOperatingSystems, then use it in this version of the main query:

    SELECT
        sub.[Operating System], 
        ConcatRelated("[Machine Name]", "TableA", 
            "[Operating System] = '" & sub.[Operating System] & "'") AS Machines
    FROM qryDistinctOperatingSystems AS sub;
    

提交回复
热议问题