SQL in excel with QueryTables

edt123

New Member
Joined
Feb 13, 2007
Messages
1
I have the following code:

With ActiveSheet.QueryTables.Add(Connection:="ODBC;DSN=[dbname];", _
Destination:=Range("A1"))
.CommandText = Array( _
"SELECT field1, field2, field3, field4, field5, field6,
"FROM Table" _
, "99S" & Chr(13) & "" & Chr(10) & "WHERE (field1='something') AND (field2='123')" _
)
.Name = "Query from [dbname]"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = True
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.Refresh BackgroundQuery:=False
End With

I would like to change the "AND (field2='123')" part to "AND (field2 in (123,321,456)) but i want to get this list from a cell with the exact text (i.e. (1,2,3)).

Please help!
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)

Forum statistics

Threads
1,225,267
Messages
6,183,946
Members
453,198
Latest member
VB6 Programming

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