我创建了一个宏,用于搜索今年接受培训的所有员工。 我在SQL方面很弱,我需要一些帮助。
目前我的宏工作原理是它在Excel中创建3列并在其中加载所有数据
Name||First Name||TrainingName
Employee1.Name||Employee1.FirstName||TrainingName1
Employee1.Name||Employee1.FirstName||TrainingName2
Employee2.Name||Employee2.FirstName||TrainingName1
Employee3.Name||Employee3.FirstName||TrainingName1
我希望数据显示如下:
Name||First Name||TrainingName1||TrainingName2 etc..
Employee1.Name||Employee1.FirstName||TrainingName1||TrainingName2 etc..
如何更改SQL行以获得我想要的内容。
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
感谢您的帮助!
答案 0 :(得分:0)
嗨,请检查一下,我有一些问题
INSERT INTO [TABLE]([姓名,名字,培训],[员工1,员工1,培训1],[员工1,员工1,培训2],[员工2,员工2,培训1],[员工3,员工3,培训1])价值观(@ [姓名,名字,培训],@ [员工1,员工1,培训1],@ [员工1,员工1,培训2],@ [员工2,员工2,培训1],@ [员工3,员工3,训练1])