mardi 23 juin 2015

How to LOAD data from recordset into separate columns?

I have created a macro which search for all employees who where on trainings this year. I'm weak in SQL and I need somebodys help.

Currently my macro works like this that it creates 3 column in Excel and loads in it all the data

Name, First Name, Training
Employee1,Employee1,Training1
Employee1,Employee1,Training 2
Employee2,Employee 2,Training1
Employee3,Employee 3,Training1

I want that the data will be showed like this: Name, First Name, Training1, Training 2 etc.. Employee1,Employee,Training,Training 2 etc.. How to change the SQL line to get what I want.

Sub TRaining()
Dim con As ADODB.connection
Dim rs As ADODB.Recordset
Dim path1 As String, SQLstr As String, conString As String, i

With Application.FileDialog(msoFileDialogFilePicker)
.Title = "GDZIE JEST PLIK?"
.Show
path1 = .SelectedItems(.SelectedItems.Count)
End With
Set con = New ADODB.connection
conString = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
               "Data Source=" & path1 & ";" & _
               "Extended Properties=Excel 12.0"
con.Open conString
SQLstr = "SELECT o.[Last Name] AS [LastName], o.[First Name] AS [FirstName], s.[TRAINING] AS [T0] FROM [pracownicy$] o LEFT JOIN [szkolenia$] s ON s.[GUID]=o.[GUID] WHERE s.[GUID] IS NOT NULL"
Set rs = New ADODB.Recordset

rs.Open SQLstr, con, adOpenUnspecified, adLockUnspecified
With ThisWorkbook.ActiveSheet
                For i = 0 To rs.Fields.Count - 1
                    ActiveSheet.Cells(1, i + 1).Value = rs.Fields(i).Name
                Next i

Range("A2").CopyFromRecordset rs
End With
End Sub

Thank you for your help!

Aucun commentaire:

Enregistrer un commentaire