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

前端 未结 2 1907
無奈伤痛
無奈伤痛 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 "".

提交回复
热议问题