Passing Parameter to SQL Query using a Question Mark

ExcelGuyDude

Board Regular
Joined
Feb 4, 2015
Messages
205
I'm using Excel 2010. I'm getting an error every time I try and pass a parameter straight through to my SQL query so I don't have to use VBA (very long SQL Code). I'm using an ODBC connection that I connected with the "From Microsoft Query" tool that's available. What I'm trying to do is use the question mark feature to replace the conditions I have currently set (W1 and 100000).

Code:
SET NOCOUNT ON

SELECT ib.Item#
	, ib.AvailQty
INTO #InventoryTable
FROM ITEM_INVENTORY_BALANCES ib WITH(NOLOCK)
WHERE ib.Whse# = 'W1'
	AND ib.AvailQty > 100000

SELECT *
FROM #InventoryTable

DROP TABLE #InventoryTable

When running this code, everything works fine.

What I'd like the code to look like is this:

Code:
SET NOCOUNT ON

SELECT ib.Item#
	, ib.AvailQty
INTO #InventoryTable
FROM DATAWSQL.dbo.ITEM_INVENTORY_BALANCES ib WITH(NOLOCK)
WHERE ib.Whse# = ?
	AND ib.AvailQty > ?

SELECT *
FROM #InventoryTable

DROP TABLE #InventoryTable

When I run try and run this query with the new changes, it gives me these 2 errors:

[Microsoft][ODBC SQL Server Driver]Invalid parameter number
[Microsoft][ODBC SQL Server Driver]Invalid Descriptor Index

I'm not sure if there's a setting I need to activate in Excel, or if this is not even possible to do with Temp tables within the SQL code.

Also, I know it's possible to put the SQL code inside of MS Access then do a pass-through query to Excel. I'm avoiding MS Access on purpose.

Thanks in advance for your help,
GuyDude
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.

Forum statistics

Threads
1,215,482
Messages
6,125,060
Members
449,206
Latest member
Healthydogs

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