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

前端 未结 2 1870
無奈伤痛
無奈伤痛 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:16

    This is a fairly basic VBA function that will loop through every row in a column, and concatenate it to a comma-delimited result string. i.e., for your example, it will return "Server01, Server02, Server03, Server04, Server05". (Don't forget to replace the column and table names)

    Function ConcatColumn(OS As String) As String
        Dim rst As DAO.Recordset
        Set rst = CurrentDb.OpenRecordset("Select * from TableA")
    
        Dim result As String
    
        'For every row after the first, add a comma and the field value:
        While rst.EOF = False
            If rst.Fields("Operating System") = OS Then _
                result = result & ", " & rst.Fields("MyValue")
            rst.MoveNext
        Wend
    
        'Clean it up a little and put out the result
        If Left(result, 2) = ", " Then result = Right(result, Len(result) - 2)
        Debug.Print result
        ConcatColumn = result
    End Function
    

    To use this,
    1. ConcatColumn("Windows") will return "Server04, Server03"
    2. ConcatColumn("Linux") will return "Server01, Server02"
    3. ConcatColumn("Solaris") will return "Server05"
    4. ConcatColumn("") will return "".

    0 讨论(0)
  • 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;
    
    0 讨论(0)
提交回复
热议问题