link sql database field to column

In column C we have the item number and in column D the description.  Is there a way to make a column C cell scrollable through a list of item numbers found in a database, and after you have found the item number you want column D shows the description for that item number.  The information we want to look at is in a sql database.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Glenn RayExcel VBA DeveloperCommented:
Just thinking quickly:  You could set up an external connection to your SQLServer db and import that field to another sheet.  Then set up data validation to point to that field/column so that it can be used on your master sheet here.
James ElliottManaging DirectorCommented:
There's a number of ways to do this.

I personally would go Data=>From Other Sources=>SQl Server

Enter your server details and choose your table or view.

Then when the data comes in and it asks how you want to import data (table, pivot etc.), click properties=>definition. Change command type from table to SQL, and then write a SQL statement using '?' where you want the parameter to be. Click ok, and then click properties again, and the parameters button should now be enabled.

You can then link the '?' in your query to a location in your spreadsheet. This location will be where you have setup a cell with a validation list containing all possible item numbers.

Hope this helps.
TomBallaAuthor Commented:
I'm trying to connect to the database, but it is not listed.  Several other databases are listed, but not the one I need.  Why would that be?
OWASP: Forgery and Phishing

Learn the techniques to avoid forgery and phishing attacks and the types of attacks an application or network may face.

James ElliottManaging DirectorCommented:
You may need to just type it in.
TomBallaAuthor Commented:
Okay, I got the database table to show in the excel sheet, but it showing every field in the database table.  How do I just select two of the fields to show?
Glenn RayExcel VBA DeveloperCommented:
You'll change your connection Command Type from "Table" to SQL and then enter a SQL statement to just select the two fields.

1) Click any cell in your Excel Table.
2) Excel Menu:  Data - Connections
You'll see a new dialog box:
Workbook Connections dialog3) Click the "Properties" button
The Connection Properties dialog box will appear:
Connection Properties dialog4) Click the "Definition" tab
5) Change the Command type to "SQL"
6) Enter a SQL statement that selects just the two fields you wish,
7) Click the "OK" button
8) Click the "Close" button
If the table does not display only the fields requested, click the "Refresh all" Icon in the "Connections" section of the ribbon.

TomBallaAuthor Commented:
Alright, that's looking good.  Is there a way to show a stockcode more than once?  The report will sometimes need to look like this.

Griffins  45644     512VTF   Red Candle  
Johnny   45645     512VTF   Red Candle
Billy's     45646     510VTF   Blue Candle

Glenn RayExcel VBA DeveloperCommented:
I'm not quite sure what you're requesting now.  Going back to your original question, i would have thought you would set up a data validation rule so that the items in column C (stock code) would be limited to those resulting from the SQL connection.  Then, you could create a VLOOKUP formula in column D to return the description, based on the code shown in column C.

You may have to provide an example workbook if this is not clear to you.

TomBallaAuthor Commented:
We want our loading dock to be able to scroll through our list of items and select an item number, and when they select that item number the description shows up in the next cell over.
Glenn RayExcel VBA DeveloperCommented:
Then, you would indeed set up data validation and a lookup formula in the cells in column C and D.

Since you have a SQL connection that resides in an Excel Table, you can use Structured Referencing to dynamically show the stock codes in a data validation pick list.  If you add, change, or remove stock codes from the table (i.e., the SQL table is updated), the Data Validation will automatically follow this.

You do this by highlighting all the cells in column C on your order sheet, then select Menu: Data -- Data Validation.  Change "Allow" to "List" then enter a formula like this in the "Source" box:
where "Table2" is the name Excel has assigned your SQL table output and [StockCode] is the fieldname (you must include brackets and the double quotes around it all).  If you aren't sure of the table name, click any cell in the SQL table and then click Menu: Table Tools - Design.  Look on the top left of the contextual ribbon and you'll see the name in the box labeled "Table Name".

When that's done, you can then add a VLOOKUP formula to return the description.  In the first cell in column D, insert this formula and then copy down:

This will display the description if a stock code is chosen in column C.

See the example workbook for more guidance.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
TomBallaAuthor Commented:
Thank you very much for your time, Glenn.  I got it working.
Glenn RayExcel VBA DeveloperCommented:
Cool.  There are plenty of other methods available for dynamic data validation, but once you have a grasp of structured referencing for tables, it's pretty flexible.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.