ADO to Union data from Excel and data from Text file

lovallee

Board Regular
Joined
Jul 8, 2002
Messages
220
Good day,

Question:
Using ADO (via VBA), is it possible for one single SQL query to simultaneously connect to both an Excel file and a .txt file? I would then do a UNION ALL operation to merge both sets of data.

Additional details:
I use ADO (via VBA) to connect to an Excel file as follow.
It works quite fine to connect to two or more Excel files with the same query by using an SQL string such as:

SELECT * FROM [Sheet1$] UNION SELECT * FROM [Sheet1$] IN 'C:\ADOTest.xls' 'Excel 8.0;'

Excerpt only
Code:
MainSourceFile = ActiveWorkbook.Path & "\" & ActiveWorkbook.Name

szConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
                    "Data Source=" & MainSourceFile & ";" & _
                    "Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"";"

szSQL = Range("SQLcommand").Value2
   
Set rsCon = CreateObject("ADODB.Connection")
Set rsData = CreateObject("ADODB.Recordset")
   
rsCon.Open szConnect
rsData.Open szSQL, rsCon, adCmdText



Thank you!

Louis
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Solved: ADO to Union data from Excel and data from Text file

I finally found it.
Default "extended properties" are set in the connection string.
Specific files "extended properties" can be set for each file accessed by the query directly in the SQL string.


Example where the default "Extended properties" are set to Excel 8.0 and file specific properties are set to text file:

Code excerpt
Code:
szConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
                    "Data Source=" & MainSourceFile & ";" & _
                    "Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"";"

SQL string:

SELECT * FROM Table1InMyExcelFile 'File properties set in the connection string
UNION ALL
SELECT * FROM [Text;Database=\\MyFolder\;HDR=YES;FMT=Delimited].[MyCSVFile.csv]

Louis
 
Upvote 0

Forum statistics

Threads
1,223,632
Messages
6,173,472
Members
452,516
Latest member
archcalx

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top