Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 108
  • Last Modified:

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.
0
TomBalla
Asked:
TomBalla
  • 5
  • 5
  • 2
1 Solution
 
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.
0
 
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.
0
 
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?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
James ElliottManaging DirectorCommented:
You may need to just type it in.
0
 
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?
0
 
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.

-Glenn
0
 
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

Thanks.
0
 
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.

Regards,
-Glenn
0
 
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.
0
 
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:
=INDIRECT("Table2[StockCode]")
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:
D2: =IFERROR(VLOOKUP(C2,Table2,2,FALSE),"")

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

See the example workbook for more guidance.

Regards,
-Glenn
EE-DataValidation-Lookup.xlsx
0
 
TomBallaAuthor Commented:
Thank you very much for your time, Glenn.  I got it working.
0
 
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.

-Glenn
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now