Home:ALL Converter>SQL From Excel VBA: Create a temporary table in VBA (for reuse) from a SQL query

SQL From Excel VBA: Create a temporary table in VBA (for reuse) from a SQL query

Ask Time:2017-06-01T15:51:42         Author:VegasVed

Json Formatter

I have been going nuts looking for a straightforward way to do this, but most answers I have come across go off on a tangent instead of directly addressing the question.

Here is a simplified SQL snippet I am trying to run through VBA

SELECT * INTO ##Customers FROM Addresses 
SELECT * FROM ##Customers a LEFT JOIN PostageRate b ON a.ZipCode=b.ZipCode 

And then I need to put all this data into an Excel sheet.

So far I am able to execute:

SELECT * FROM Addresses 

This is the functional equivalent of the first query without the temporary table. I land up with a RecordSet which I then put into a sheet using CopyFromRecordSet, but I don't know how to turn this RecordSet into a table so I can query it for the second query where I do the LEFT JOIN and then put the resulting RecordSet on the sheet.

Naturally the queries I am trying to run are more complex than this one, but this illustrates what I am trying to achieve.

Apologies if I have missed a post that does answer this, but I have yet to come across it.

Thanks!

EDIT: I have looked into putting the data from the first query onto a sheet and treating the sheet as a table for the second query. The problem is as follows: The sheet is local and the second table is on a database. According to this question, the database would need to have access to the Excel sheet. It does not. Any more insights about how to achieve temporary tables through VBA would be greatly appreciated. Thanks all!

Author:VegasVed,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/44301650/sql-from-excel-vba-create-a-temporary-table-in-vba-for-reuse-from-a-sql-query
yy