access sql query to concatenate rows

前端 未结 2 449
执念已碎
执念已碎 2020-12-22 05:36

I have following data in csv format which I exported in access format

House Number | Road name | Postcode
1 | alex road | sw2 4r
2 | alex road | sw2 4r
3 | a         


        
2条回答
  •  一生所求
    2020-12-22 06:06

    As was pointed out by onedaywhen in an early post on SO, this is easier with ADO, there is an example here

    Sample query:

       SELECT d.DeptID, d.Department, 
              ConcatADO("SELECT FName & ' ' & SName, Address FROM Persons 
                         WHERE DeptID=" & [d].[DeptID],", "," : ") AS Who
       FROM Departments AS d INNER JOIN Persons AS p ON d.DeptID = p.DeptID
       GROUP BY d.DeptID, d.Department, 3;
    

    Function using ADO

    Function ConcatADO(strSQL As String, strColDelim, strRowDelim, ParamArray NameList() As Variant)
       Dim rs As New ADODB.Recordset
       Dim strList As String
    
       On Error GoTo Proc_Err
    
           If strSQL <> "" Then
               rs.Open strSQL, CurrentProject.Connection
               strList = rs.GetString(, , strColDelim, strRowDelim)
               strList = Mid(strList, 1, Len(strList) - Len(strRowDelim))
           Else
               strList = Join(NameList, strColDelim)
           End If
    
           ConcatADO = strList
    
       Exit Function
    
       Proc_Err:
           ConcatADO = "***" & UCase(Err.Description)
       End Function
    

提交回复
热议问题