Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / New Users / April 2004

Tip: Looking for answers? Try searching our database.

Passing Cell values to a SQL statement

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Tyler WIllis - 05 Apr 2004 16:43 GMT
I have set up a spreadsheet that uses excel's "Get external data"
feature to connect to a SQL database and run a querry. The problem is
that I need to get data for several different product codes. These
codes change from time to time but basically the querry looks like
this

Select * from SalesData
Where
SalesData.productCode = 1
Or
SalesData.productCode = 2

is there a way to set it up such that the product codes are passed to
the statement from cell values in a given range? This way it would
dynamically retrive whatever I am looking for.

Also if any of you double as SQL wizzards, can I pass an array of
values into my Where clause so that I don't have to list each
sepperately?

Thanks.

Tyler
Arvi Laanemets - 05 Apr 2004 18:24 GMT
Hi

You need a procedure which will rewrite the CommandText propertie for querie
object. Here is an example
Public Sub UuendaP?ringud()
   kuu = ActiveSheet.Range("F1").Value
   aasta = ActiveSheet.Range("F2").Value
   ...
   Set qtQtrResults = Worksheets("Tasud").QueryTables(1)
   Sheets("Tasud").Activate
   ActiveSheet.Range("A2").Select
   With qtQtrResults
       .CommandType = xlCmdSql
       .CommandText = _
           "SELECT a.tabn, SUM(a.summa) AS arvest, "&_
           "SUM(IIF(a.sotsmaks,1,0)*a.summa) AS sotsalus, "&_
           "a.allyksus FROM tasud a "&_
           "WHERE a.tabn<>'' AND a.summa>0 AND a.tl<'060' AND "&_
           "Year(a.sisestus)=" & aasta & " AND "&_
           "Month(a.sisestus)=" & kuu & " "&_
           "GROUP BY a.allyksus, a.tabn ORDER BY a.tabn"
       .Refresh
   End With
....
End Sub

--
Arvi Laanemets
(Don't use my reply address - it's spam-trap)

> I have set up a spreadsheet that uses excel's "Get external data"
> feature to connect to a SQL database and run a querry. The problem is
[quoted text clipped - 19 lines]
>
> Tyler
onedaywhen - 06 Apr 2004 11:18 GMT
You can use the IN keyword i.e.

 Select * from SalesData
 Where SalesData.productCode IN(1,2)

Performace will depending on which DBMS you are using, so do some
testing if you intend to use

Again depending on DBMS product, it is preferable to create a stored
procedure on the server side and get the client (Excel) to pass the
values for the IN clause as parameters (might get away with passing as
a single string paramter?)

--

> I have set up a spreadsheet that uses excel's "Get external data"
> feature to connect to a SQL database and run a querry. The problem is
[quoted text clipped - 19 lines]
>
> Tyler
Tyler WIllis - 06 Apr 2004 22:10 GMT
Thanks, Exactly the sort of answers I was looking for. Sorry for not
getting this into the right section. I didn't notice the ADO thread
till after I posted.

> You can use the IN keyword i.e.
>
[quoted text clipped - 34 lines]
> >
> > Tyler
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.