Can anyone help me with this please.
I want to do something like this.
strSQL = SELECT [filename1].[" & xlSheet.Name & "$].Col1
, [filename1].[" & xlSheet.Name & "$].Col2
FROM [filename1].[" & xlSheet.Name & "$]
UNION ALL
SELECT [filename2].[" & xlSheet2.Name & "$].Col1
, [filename2].[" & xlSheet2.Name & "$].Col2
FROM [Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filename2+ ";Extended Properties=Excel 12.0;HDR=Yes].[" & xlSheet2.Name & "$]
WHERE [filename1].[" & xlSheet.Name & "$].Col1= [filename2].[" & xlSheet2.Name & "$].Col1
Using conExcel As New OleDb.OleDbConnection()
conExcel.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filename1 + ";Extended Properties=Excel 12.0;HDR=Yes"
conExcel.Open()
cmdSelect.CommandText = strSQL
cmdSelect.Connection = conExcel
intRowsCount = cmdSelect.ExecuteNonQuery()
You cannot address two different files with the one OleDb connection. You’ll need to create another connection for the second file and pull the data in that way.
Here’s how I usually code a method to retrieve all data from a sheet:
And then you can just call it with each of your files: