View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Rob Bovey Rob Bovey is offline
external usenet poster
 
Posts: 811
Default SQL Database Query


In order to do this with MS Query you have to 1) create a parameter
query, 2) provide the initial parameter manually, 3) dump the data back to
Excel, then 4) edit the query to point the parameter at your source data
cell.

The SQL for the parameter query looks like this:

SELECT *
FROM table1
WHERE (table1.date=?)

Note the question mark in the WHERE clause. When you enter this query, you
will be prompted for a parameter value. Give it some initial value and then
return the data to Excel.

Next, right-click on your data table and choose Parameters from the
shortcut menu. In the Parameters dialog select the "Get the value from the
following cell" option. Select the cell you want the value to come from and
optionally check the box that updates the data whenever that cell's value
changes, then click OK. Your query should now be connected to your input
cell.

--
Rob Bovey, Excel MVP
Application Professionals
http://www.appspro.com/

* Take your Excel development skills to the next level.
* Professional Excel Development
http://www.appspro.com/Books/Books.htm

"Zinewowe" wrote in message
...
Hi,
I have used VB before to retrieve data from a SQL database by using a
string.
All want to do here is using Microsoft Query to retrieve data from a SQL
database into Micorsoft Excel.

Do do so I entered the following string into the Microsoft query SQL
statement editor:
SELECT *
FROM table1
WHERE (table1.date='A1')

A1 is simply the field in the Excel speadsheet which I would like to use
to
difine the date which is then used in the WHERE statement.
So what I am doning is trying to pass on a parametervalue from the
A1-field
to the SQL statement.
Obviously something with 'A1' (how I pass on the parameter) is wrong.

Thanks once more!





"Rob Bovey" wrote:


You don't say how you are trying to retrieve this data, but since
this
is a programming forum I'll assume you're trying to use VBA. In that
case,
you'll need to specify the SQL statement as a string in order to do
anything
with it. One way to do that, which includes a demonstration of how to
collect the date from your worksheet cell, is the following:

Dim szSQL As String

szSQL = "SELECT * " & _
"FROM Table1 " & _
"WHERE (" & Sheet1.Range("A1").Value & "='07/07/2005')"

--
Rob Bovey, Excel MVP
Application Professionals
http://www.appspro.com/

* Take your Excel development skills to the next level.
* Professional Excel Development
http://www.appspro.com/Books/Books.htm

"Zinewowe" wrote in message
...
I want retreive data from Table1 from a SQL database which match the
WHERE
criteria:

SELECT *
FROM Table1
WHERE (date1='07/07/2005')

date1 is a field in Excel which I use to define the date for the WHERE
criteria.

I tried the following but it did not work (I entered the date value in
field
A1 in the Excel sheet):
SELECT *
FROM Table1
WHERE ('A1'='07/07/2005')

What am I doing wrong?
Thanks