VBA SQL query using input values from worksheet pull-down

dcg38524

Board Regular
Joined
Dec 4, 2013
Messages
113
Hi,

I am attempting (with no luck :biggrin:) to build a SQL query that would accept an input value from a pull-down menu from existing workbook.

The input value would change per. pull down selection via. "Sheet1 - E7".

Example:

SELECT MODEL
FROM SQL_Table
WHERE
MODEL in ("Sheet1"[e7])



Any help would be greatly appreciated.

Regards,
Don
 
Last edited:

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Thanks Fazza & Mark, for responding and posting the links.

Unfortunately, the links did not shed any light on what I was really looking for. Good news, with a lot of trial and error :rolleyes: I was able to stumble across a solution to my original posted question.


Solution:
SELECT MODEL
FROM SQL_Table
WHERE
MODEL in '" & Sheets("Sheet1").Range("e7").Value & "'

With the above entry a user can select from a pull down menu a valid "model name" located in workbook "Sheet1.e7" and a database query will provide the results.

Regards,
Don
 
Upvote 0
Good work, Don.

Some comments, maybe these help

It is preferred to use a worksheet's code name instead of actual name - the actual name might be changed by a user but the code name less likely. If the user changes the name of "Sheet1" to "Report" then the VBA reference to the non-existent "Sheet1" will give an error. You can see the code name in the visual basic editor, or could even debug.print it something like
? sheets("Sheet1").codename

Range "E7" might move if rows or colums are added/deleted in the worksheet: then the cell you want to refer to will no longer be "E7", it might move say to "F8". With "E7" hard coded in the VBA, the code will still refer to "E7" and hence not have the right value. Normal practice is to use a defined name. So give cell "E7" a name, CTRL-F3, like "SelectedModel" and then in the code use sheetcodename.range("SelectedModel").value

Normal SQL WHERE clause syntax options, for info

WHERE MODEL = 'this text'
WHERE MODEL = 'this text' OR MODEL = 'other text'
WHERE MODEL = 123
WHERE MODEL IN ('some text')
WHERE MODEL IN ('this text', 'other text', 'further text', 'more text')
WHERE MODEL NOT IN ('this text', 'other text', 'further text', 'more text')
WHERE MODEL IN (123, 234, 345, 456)

cheers
 
Last edited:
Upvote 0
Fazza,

I love it, excellent advice. I begin will implementing your suggestions ASAP thank you.

Kind regards,
Don
 
Upvote 0

Forum statistics

Threads
1,214,644
Messages
6,120,709
Members
448,983
Latest member
Joaquim_Baptista

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top