Hello again,
I'm completely new at using SQL from VB. I'm trying to merge 2 tables (I connect successfully to them) based on a common key. This is the code I am using but I naturally get errors. Could you help me getting to the right direction?
Sub RefeWbk()
Dim rsData As ADODB.Recordset
'Create the connection string.
Dim oConn As New ADODB.Connection
oConn.Open "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=C:\Dropbox\word\VlookupPlus\VlookupTestFile.xls;" & _
"Extended Properties=""Excel 12.0;HDR=NO;IMEX=1"";"
WhereTo = "A1" ' First cell in the range to paste to
'Master table
Set rsData = New ADODB.Recordset
rsData.Open "Select * from [DatForSAS (5)$A1:T3100]", oConn, adOpenStatic
'Lookup table
Set LKdata = New ADODB.Recordset
LKdata.Open "Select * from [ListFromPop (2)$A1
410]", oConn, adOpenStatic
'Merged table
Dim rs As New ADODB.Recordset
Set rs = New ADODB.Recordset
rs.Open "SELECT rsData.a001, LKdata.MgrLevel", oConn, adOpenStatic 'Error!!!!
'Merging query
Dim cm As New ADODB.Command
Set cm = New ADODB.Command
With cm
.ActiveConnection = oConn
.CommandTimeout = 300
.CommandType = adCmdText
.CommandText = "From rsData" & "INNER JOIN LKdata" & "ON rsData.EMPnum=LKdata.EMPnum"
.Execute 'Error!!!!
End With
ActiveSheet.Range(WhereTo).CopyFromRecordset rs
End Sub
Thanks for your help
Avi