Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default modify query with value in combo box

Is it possible to change the value of sql query with a combo box. for
example change the where statment to a variable in a combo box. where
company = usamobile or something like that.

I found this link
http://www.rdg.ac.uk/ITS/info/traini...s/excel/query/

but it looks like what is doing is filter the result.

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,163
Default modify query with value in combo box

Yes but exact technique depends on how you set up the query - specifically is
it a QueryTable you set up with MSQuery or did you write code with ADO? But
a SQL query is just a string, so you can use all string functions to set one
up or modify it.

The simple way is to use MSQuery to set up the query with a parameter (if
you don't know how, check help) and use a cell value as your parameter. Then
have the combobox value go into that cell.

But if you need to have more flexibility, you can do it all through VBA
code. Suppose you used MSQuery so the results are in
Sheets("Sheet1").QueryTables(1). The CommandText property contains the SQL
string. Let's say you set it up to select usamobile and now you want
verizon. Assuming it does not have the word usamobile in it anywhere else, I
could just use the Replace function:
With Sheets("Sheet1").QueryTables(1)
.CommandText = Replace(.CommandText,"usamobile","verizon")
.Refresh
End With

Since your combobox would contain the value to use, you could use this code
instead to reset the value of the SQL string.
..CommandText = Replace(.CommandText,"usamobile",ComboBox1.Value)

If you need to set this up for continual changes, though, the text to
replace will not always be "usamobile." How to find and replace the name
depends on the actual SQL string. I would probably set it initially to some
'dummy' value, like "COMPANYX" and then do this:
With Sheets("Sheet1").QueryTables(1)
.CommandText = Replace(.CommandText,"COMPANYX",ComboBox1.Value)
.Refresh
.CommandText = Replace(.CommandText,ComboBox1.Value,COMPANYX")
End With

This preserves the "dummy" value in the SQL string after the refresh so next
time I need to change it I can find and change it easily using the same code.
--
- K Dales


" wrote:

Is it possible to change the value of sql query with a combo box. for
example change the where statment to a variable in a combo box. where
company = usamobile or something like that.

I found this link
http://www.rdg.ac.uk/ITS/info/traini...s/excel/query/

but it looks like what is doing is filter the result.


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default modify query with value in combo box

Thanks I think I got it to work, I managed to get the query to
refresh, when my combo box changes too. I was able to specify the
parameter after I recreate the query with the ms query wizard.

K Dales wrote:
Yes but exact technique depends on how you set up the query - specifically is
it a QueryTable you set up with MSQuery or did you write code with ADO? But
a SQL query is just a string, so you can use all string functions to set one
up or modify it.

The simple way is to use MSQuery to set up the query with a parameter (if
you don't know how, check help) and use a cell value as your parameter. Then
have the combobox value go into that cell.

But if you need to have more flexibility, you can do it all through VBA
code. Suppose you used MSQuery so the results are in
Sheets("Sheet1").QueryTables(1). The CommandText property contains the SQL
string. Let's say you set it up to select usamobile and now you want
verizon. Assuming it does not have the word usamobile in it anywhere else, I
could just use the Replace function:
With Sheets("Sheet1").QueryTables(1)
.CommandText = Replace(.CommandText,"usamobile","verizon")
.Refresh
End With

Since your combobox would contain the value to use, you could use this code
instead to reset the value of the SQL string.
.CommandText = Replace(.CommandText,"usamobile",ComboBox1.Value)

If you need to set this up for continual changes, though, the text to
replace will not always be "usamobile." How to find and replace the name
depends on the actual SQL string. I would probably set it initially to some
'dummy' value, like "COMPANYX" and then do this:
With Sheets("Sheet1").QueryTables(1)
.CommandText = Replace(.CommandText,"COMPANYX",ComboBox1.Value)
.Refresh
.CommandText = Replace(.CommandText,ComboBox1.Value,COMPANYX")
End With

This preserves the "dummy" value in the SQL string after the refresh so next
time I need to change it I can find and change it easily using the same code.
--
- K Dales


" wrote:

Is it possible to change the value of sql query with a combo box. for
example change the where statment to a variable in a combo box. where
company = usamobile or something like that.

I found this link
http://www.rdg.ac.uk/ITS/info/traini...s/excel/query/

but it looks like what is doing is filter the result.



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help to modify the query? Eric Excel Worksheet Functions 0 March 11th 08 05:54 AM
Modify a data query ?? CM Excel Programming 2 November 10th 05 05:20 PM
Stop to modify the SQL query manually entered into query ! Olivier Rollet Excel Programming 6 November 3rd 04 08:34 AM
Any Idea on how to modify this code relating to query??? hce[_24_] Excel Programming 1 October 14th 04 12:27 PM
Modify Web Query using macros? SPYREN Excel Programming 2 October 11th 04 03:37 PM


All times are GMT +1. The time now is 06:41 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"