I have a working Excel spreadsheet which uses VBA to change a parameter in the connection query depending on what is entered into a single cell. It cannot use an Excel Parameter to pick up the value because the variable is in the join not in the where part of the query. So I know this works in principle, at least for one cell of data.
I now need to create a new spreadsheet where I need to put a range of data into the query.
The query looks something like this:
Select * FROM TABLE_A Where ID in ('A', 'B', 'C')
The VBA picks up the values A, B and C from a column in the spreadsheet, and currently looks like this:
Dim ID_Range As Range
Sheets("Data").Select
Set ID_Range = Sheets("Data").Range("A1:A10")
With ActiveWorkbook.Connections("Query from Database_A").ODBCConnection
.BackgroundQuery = True
.CommandText = Array( _
"Select * FROM Table_A A WHERE A.ID in " "(" + ID_Range + ")")
.CommandType = xlCmdSql
.Connection = Array(Array( _
ODBC;Description= ****
.RefreshOnFileOpen = False
.SavePassword = False
.SourceConnectionFile = ""
.SourceDataFile = ""
.ServerCredentialsMethod = xlCredentialsMethodIntegrated
.AlwaysUseConnectionFile = False
End With
I hope I haven't removed any important code when removing the company specific information.
When this is run it comes up with the error: Subscript out of Range.
What do I need to do to get this to work?