tags:

views:

71

answers:

2

PROBLEM: I have a few worksheets that use sql queries to retrieve data. I would like all of these sheets to query data based on the same date range entered by a user in excel.

eg. I would enter somewhere in a form or in a cell a date range and retrieve the value in sql to obtain data based on the user specified dates.

Is there anyway to access values from forms or from cells in SQL (Query editor perhaps)?

Thanks!

A: 

In MS Query (which you use to build a QueryTable) set the tick to display the Criteria grid, and for required fields enter something in brackets as a criterion value:
A screenshot

This will create two parameters in the query. Save and return to Excel, right-click the QueryTable, click Parameters... and point them to the right cells.

GSerg
Where is this "set the tick to display the Criteria grid"?thanks!
Ehsan
`View -> Criteria`
GSerg
I figured it was simple but I'm using Excel 2003 and I do not have Criteria enabled for some reason. What is the condition that might make this enabled. I have MS Query open, I'm looking at my table which I queried and most menu options are gray-ed out. Any thoughts?Thanks
Ehsan
I opened MS Query by going to my sheet which has external data and I chose "Edit Query" from the Data menu and so most of my menu options are disabled.
Ehsan
Do you get the `This query cannot be represented graphically` message by any chance? Parameters are not supported for such queries. And what is your data source?
GSerg
Yea I get that message and I continue anyway. My data source is a sql database on my sql server. How do I write a query for excel so I don't get that message. Or rather, how do I write a query so I can use criteria on it.
Ehsan
MS Query is quite crappy at building queries. You want either to make your query so simple that MS Query can represent it graphically (and therefore can allow parameters on it), or to move this query to the server by creating a view based on your `select` statement, and then using a mere `select * from this_view` in MS Query (which obviosuly will allow parameters). Don't worry about the fact that the view will return all records without filtering -- SQL Server is smart enough to apply the parameters from Excel before executing the `select`.
GSerg
Ah ok thanks that cleared it up well, I'm doing a complex query so that explains why I can't do it graphically. Thanks GSerg, great info.
Ehsan
+2  A: 

I think this example might be what you are looking for?

Parameters in Excel external data queries

MrUpsideDown