SQL pivot function text file database with VBA Excel

后端 未结 2 1237
醉话见心
醉话见心 2021-01-24 12:11

We do not have access to SQL server at work so I have to design an app within Excel VBA and using a text file (CSV) to store the data.

I have no problem querying data, j

2条回答
  •  春和景丽
    2021-01-24 13:12

    Public Function getData(db1 As String, db2 As String, var1 As String, var2 As String) As ADODB.Recordset
    
    Dim path As String, conn As ADODB.Connection, rs As ADODB.Recordset
    path = ThisWorkbook.path & "\"
    
    Set conn = New ADODB.Connection
    Set rs = New ADODB.Recordset
    
    conn.Open ("Provider=Microsoft.Jet.OLEDB.4.0;" & _
               "Data Source=" & path & ";" & _
               "Extended Properties=""text; HDR=Yes; FMT=Delimited; IMEX=1;""")
    
    rs.ActiveConnection = conn
    
    rs.Source = "TRANSFORM sum(a.allocation) " & _
                "SELECT a.emp_id, b.name, b.[new title], b.[new department] " & _
                "FROM " & db1 & " a " & _
                "INNER JOIN " & db2 & " b " & _
                "ON a.emp_id = b.emp " & _
                "GROUP BY a.emp_id, b.name, b.[new title], b.[new department] " & _
                "ORDER BY b.[new department], b.[new title], b.name " & _
                "PIVOT a.client"
    
    Set getData = rs
    

    End Function

    Transform ended up doing what I needed.

    thanks for the help!

提交回复
热议问题