Question : Dynamic Drop-Down (Sql populated)

How do i dynamically populate a Drop-Down box in excel with sql data?

Answer : Dynamic Drop-Down (Sql populated)

There is no direct way. Howver it is pretty easy using the External Data feature of Excel:
1- Import the data
---------------------
-Create a new sheet (later we'll hide it from the user), say HData (for Hidden Data)
-Create the query: menu Data > Import (or Get) External Data. The Data SOurce dialog pops-up.
-There, choose your source (eg: 'Excel FIles' or MS Access or a defined DSN) and continue the steps to create the query.
-Once the data have been returned to  Excel, you'll notice the returned data have been given a name (either the check at the Name box on the left of the formula bar or menu Insert > Name > Define)
-right-click on a data cell and choose Data Range Properties from the shortcut menu. There, you can set options:
   - Name of the query at the top which should be similar to the named range automatically given by Excel. Give a shorter and appropriate name, say cbChoices (for combo box choices). The query is renamed as well as the named range.
   - set it to Refresh On FIle Open
   - set it to refresh every x minute if you wish
   - make you uncheck 'Include FIeld Names'
   - maybe you want 'Preserve Cell Formatting'
   ...

Now on the sheet with the combo.

2- Link the COmbo
---------------------
The combo contents can be link to and in sync with a range of cell.
Here is the process for several types of combobox:
--->Combobox from the Forms toolbar (menu View > Toolbars > Forms)
  - right click the control, and go in tab Control
  - set InputRange: =HData!cbChoices
    (HData was the sheet getting the imported data and cbChoices the named range of the returned data on that sheet)
  - set CellLink: =sheet_name!cell_reference   <-- choose where to return the selected value
    The selected value is returned as an index within the list, therefore you can retreive the string value with the formula:
        =INDEX(HData!cbChoices, sheet_name!cell_reference, 1)
   Done!
---> Combobox from the Contol Toolbox toolbar on a Sheet
   - Right-Click the control and choose Properties from the shortcut menu. If you can't see Properties, select the control and click the Properties icon on the COntrol Toolbox toolbar. The Properties window is displayed.
   - The properties to set are similar as in the above example except they have different names:
       - ListFillRange: =HData!cbChoices
       - LinkedCell: =sheet_name!cell_reference
      In this case though the value from the selection in the combo is returned, not its index.
---> Combobox from the Contol Toolbox toolbar in a Userform
    Same as above except that the porperties to set have, again, different names:
       - RowSource: =HData!cbChoices
       - ControlSource: =sheet_name!cell_reference

3-Hide the sheet
---------------------
During testing you can hide the sheet as just Hidden (menu FOrmat > Sheet > Hide). However when distributing the book, you don't want the users to be able to unhidde the sheet through the same menu.
That is, you want to hide the shet as VeryHidden: in vba editor, select the HData sheet from the Project Explorer window, then, in the Properties window, set its Visibilty property to xlSheetVeryHidden. Unhiding the VeryHidden sheet can only be done within the vba editor (folwing this same process) or through code.

I hope this helped,
Seb
Random Solutions  
 
programming4us programming4us