all groups > sql server odbc > march 2006 >
You're in the

sql server odbc

group:

Variable SQL Statements pulling from a cell in Excel


Variable SQL Statements pulling from a cell in Excel Preacher Man
3/22/2006 11:38:01 AM
sql server odbc:
I have a situation that I hope someone can give me a good answer to. Please
bear with me as I try to explain my needs.

My goal is to have an Excel spreadsheet that pulls from a SQL database and
the the query pulls from a variable in the Excel sheet. For example let's
say I am pulling Sales Orders from SQL, let's also simply assume the record
has two fields Year and SOno.

Under a simple query I could say Select * from table. Of course that would
pull all so's from every year.

To filter the query of course I could say Select * from table where
year='2006'. And naturally that would give me 2006 orders.

OK, here's where I can't figure this out. I want the query to filter
according to a value in a cell in excel. Let's use cell A1. Essentially I
am looking for a query that says "Select * from table where Year=(Cell A1 in
the Sheet)"

Is this possible?

RE: Variable SQL Statements pulling from a cell in Excel Dan
3/22/2006 2:22:27 PM
Write a Macro in the Excel Spreadsheet which will take the cell parameters.
[A1.YEAR] which can be passed to the query.

[quoted text, click to view]
Re: Variable SQL Statements pulling from a cell in Excel Preacher Man
3/22/2006 4:29:01 PM
Could you please elaborate on the steps.

Thanks.

[quoted text, click to view]

Re: Variable SQL Statements pulling from a cell in Excel Ardus Petus
3/22/2006 8:03:36 PM
Use :
SELECT * FROM TABLE WHERE YEAR = ?

Close Microsoft Query and get back to Excel

In popup window, select Parameters...

HTH
--
AP

"Preacher Man" <nospam> a écrit dans le message de
news:OlWApedTGHA.776@TK2MSFTNGP09.phx.gbl...
[quoted text, click to view]

Re: Variable SQL Statements pulling from a cell in Excel Steen Persson (DK)
3/28/2006 12:00:00 AM
[quoted text, click to view]


Hi

I don't know if you already has got it working, but something like the
code below will do the trick.

Range("A7").Select
With Selection.QueryTable
.Connection = Array(Array( _
"ODBC;DRIVER=SQL
Server;SERVER=YourServerName;UID=UserName;APP=Microsoft Office
2003;WSID=xxxxx;DATABASE=YourDatabaseName;Network=DBMSSOCN;Trusted_Connection="
_
), Array("Yes"))
.CommandText = Array("EXEC SomeStoredProcedure " +
CStr(Range("A2")) + "," + CStr(Range("A3")))
.Refresh BackgroundQuery:=False


The above it done in a macro, and then I have a commandbutton that
executes the macro.

The first line "Range("A7").select tells where to start inserting the
result into the Excel sheet. The Range("A3") and Range("A2") paramters
tells which cell to get the values from. The above example is based on
running a stored procedure that takes 2 parameters but if you get the
idea I'm sure you can adjust it to your needs. The Cstr(....) is a
conversion to a string which I had to have to get it running.

I'm not at all an expert on Excel and/or VB so the code can most likely
be nicer...;-).

Regards
AddThis Social Bookmark Button