Excel-File:
| A | B | C | D | E
---|----------------|-----------------|-----------------------------|-----------------|------------
1 | sales_date | 2020-01-15 | =CONCATENATE(C2,C3) | |
2 | | | SQL-Query Part 1* | |
3 | | | SQL-Query Part 2* | |
4 | | | | |
VBA
Sub Get_Data_from_DWH()
Dim conn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Set conn = New ADODB.Connection
conn.ConnectionString = "DRIVER={MySQL ODBC 5.1 Driver}; SERVER=XX.XXX.XXX.XX; DATABASE=bi; UID=testuser; PWD=test; OPTION=3"
conn.Open
strSQL = Sheet1.Range("C1")
Set rs = New ADODB.Recordset
rs.Open strSQL, conn, adOpenStatic
Sheet1.Range("D1").CopyFromRecordset rs
rs.Close
conn.Close
End Sub
SQL-Query:
SELECT
product, brand, sales_channel,
country, sales_manager, sales_date, return_date,
process_type, sale_quantity, return_quantity, sales_value, variable_costs, fixed_costs
FROM bi.sales
WHERE sales_date = '"&B1&" AND country IN ('DE', 'US', 'NL')
ORDER BY FIELD (brand, 'brand_A', 'brand_B', 'brand_C');
I want to run the above SQL-Query
with the above VBA
.
The problem is that the query contains more than 255 characters so it does not fit in one Excel-Cell.
Therefore, I randomly split the query into two parts in Cell C2
and Cell C3
and used CONCATENATE(C2,C3)
in Cell C1
to combine both parts of the query.
When I now run the VBA
I get runtime error '-2147217887 (80040e21)'
.
I guess that VBA
cannot handle the CONCATENATE(C2,C3)
in Cell C1
.
Therefore, I am wondering if there is any other way to solve this issue?
NOTE:
I know I could put the SQL directly into the VBA code. However, my idea is to split the SLQ-string and the execution code since my origianl SQL has even more characters than the example in this quesion and I want to keep the VBA structured as described in this question.