Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

populate Excel dropdown with Access data

Posted on 2002-05-29
7
Medium Priority
?
668 Views
Last Modified: 2008-02-01
How would I go about populating a dropdown list for a column of cells so it populates the list from a query returned from an Access table?  Then based on what the user choooses, I want the index field to be stored in the cell rather than the description.

Thanks.
0
Comment
Question by:krolfes
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 6

Expert Comment

by:blakeh1
ID: 7042951
Here is a sample of a routine that can be called on the open of the workbook, or from a refresh button or whatever.
It queries an access database and populates the list box. An alternate apporach would be to create a MSQuery link to the db on another sheet and just use that as the ListRange value in the properties window. This sample uses a 2 column combobox, where the first column is the ID value and is not displayed. You would set the properties accordingly (I have ColumnCount = 2 and ColumnWidths = 0 pt;10 pt and my LinkedCell = Sheet1!A1)

Sub usbGetListItems()
    Dim dbe As DAO.DBEngine
    Dim ws As DAO.Workspace
    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    Dim s As msforms.ComboBox
    Dim i As Integer
    Dim lstcount As Long
    Dim varItem As Variant
    Dim objControl As Object

    Set dbe = New DAO.DBEngine
    Set ws = dbe.Workspaces(0)
    Set db = ws.OpenDatabase("D:\data\access2k\General2k.mdb")
    Set rs = db.OpenRecordset("SELECT ID, Color FROM tblColors")
   
    If rs.EOF = True And rs.BOF = True Then
        Err.Raise 55000, "usbGetListItems", "No records were returned!"
    Else
       
        rs.MoveFirst
        ' This must be a combobox added from the "Control Toolbox" toolbar,
        ' not the one from the "Forms" Toolbar
        Set objControl = Sheets("Sheet1").Shapes("ComboBox1").OLEFormat.Object
        Set s = objControl.Object
        lstcount = s.ListCount
        'remove all the items
        Do Until lstcount = 0
            s.RemoveItem 0
            lstcount = lstcount - 1
        Loop
        'add new items
        Do While Not rs.EOF
            s.AddItem
            s.Column(0, i) = rs.Fields("ID").Value
            s.Column(1, i) = rs.Fields("Color").Value
           
            rs.MoveNext
            i = i + 1
        Loop
    End If
Exit_usbGetListItems:
    On Error Resume Next
    If Not rs Is Nothing Then rs.Close: Set rs = Nothing
    If Not db Is Nothing Then db.Close: Set db = Nothing
    If Not ws Is Nothing Then ws.Close: Set ws = Nothing
    If Not dbe Is Nothing Then Set dbe = Nothing
    If Not s Is Nothing Then Set s = Nothing
    If Not objControl Is Nothing Then Set objControl = Nothing
Err_usbGetListItems:
    MsgBox "Error [" & Err.Number & "] " & Err.Source & vbCrLf & Err.Description, vbCritical, "usbGetListItems"
End Sub
0
 
LVL 6

Expert Comment

by:blakeh1
ID: 7042956
Sorry, here is the correct version

Sub usbGetListItems()
    Dim dbe As DAO.DBEngine
    Dim ws As DAO.Workspace
    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    Dim s As msforms.ComboBox
    Dim i As Integer
    Dim lstcount As Long
    Dim varItem As Variant
    Dim objControl As Object

    On Error GoTo Err_usbGetListItems
   
    Set dbe = New DAO.DBEngine
    Set ws = dbe.Workspaces(0)
    Set db = ws.OpenDatabase("D:\data\access2k\General2k.mdb")
    Set rs = db.OpenRecordset("SELECT ID, Color FROM tblColors")
   
    If rs.EOF = True And rs.BOF = True Then
        Err.Raise 55000, "usbGetListItems", "No records were returned!"
    Else
       
        rs.MoveFirst
        ' This must be a combobox added from the "Control Toolbox" toolbar,
        ' not the one from the "Forms" Toolbar
        Set objControl = Sheets("Sheet1").Shapes("ComboBox1").OLEFormat.Object
        Set s = objControl.Object
        lstcount = s.ListCount
        'remove all the items
        Do Until lstcount = 0
            s.RemoveItem 0
            lstcount = lstcount - 1
        Loop
        'add new items
        Do While Not rs.EOF
            s.AddItem
            s.Column(0, i) = rs.Fields("ID").Value
            s.Column(1, i) = rs.Fields("Color").Value
           
            rs.MoveNext
            i = i + 1
        Loop
    End If
Exit_usbGetListItems:
    On Error Resume Next
    If Not rs Is Nothing Then rs.Close: Set rs = Nothing
    If Not db Is Nothing Then db.Close: Set db = Nothing
    If Not ws Is Nothing Then ws.Close: Set ws = Nothing
    If Not dbe Is Nothing Then Set dbe = Nothing
    If Not s Is Nothing Then Set s = Nothing
    If Not objControl Is Nothing Then Set objControl = Nothing
    Exit Sub
Err_usbGetListItems:
    MsgBox "Error [" & Err.Number & "] " & Err.Source & vbCrLf & Err.Description, vbCritical, "usbGetListItems"
End Sub
0
 

Author Comment

by:krolfes
ID: 7043037
blakeh1,

I need to apply this dropdown to all cells in a column, so that when the user chooses any cell in the column, the dropdown may be chosen.

0
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 
LVL 16

Accepted Solution

by:
sebastienm earned 200 total points
ID: 7043168
Krolfes,
This may fill your needs.
No macro (vba) involved.

Sheet is divided in 2 main sections
- Columns (A,B,C) used to list possible list values
  These columns will be hidden later so that the user
  don't see them
- Columns D,E,... = user area

Process:
- Get list of Descriptions & index when file opens
- use this values in drop-down list for user

GET LIST OF DATA: Using External data (QueryTable)
--------------------------------------------------
- Select A1
- menu Data>Get External Data>New Database Query
   - on Databases tab, select MS Access Database, click OK
   - the 'Select Database' dialog pops up.
     Browse and select the Access database, click OK
   - The "Query Wizard - Choose Columns" pops up,
     Select Description & Index fields in this order
   - go through all the Query Wizzard steps: Filter Data,
     Sort Order, Finish.
   - the 'Returning Ext Data To MS Excel' dialog pops up
     Click Properties
        - Make sure
          Name = DescList   (as you which , but used later)
          Refresh Data On File Open = TRUE
             --> enables update of listing at workbook open
          Include filed names = FALSE
          Preserve Cell Formatting = TRUE
          Overwritte Existing Cells = TRUE
        - Click OK
      Click OK

You now have the list in columns A,B without headers and
the whole area of values is a named range called DescList.

SET ENTRY COLUMN: using Data Validation
-----------------------------------------------------
Say you want the user to get choose description from list
in any cell of column E.
-select all cells for entry (columnE starting ,say, row 4)
-menu Data>Validation, tab Settings:
   Allow = List
   Source = =DescList
   You can also play with tab Input Message and
   Error Alert    
-Create an new column, say F, which will contain the index
 =VLOOKUP(E4,DescList,2,False)

CLEAN UP
---------------------------------------------------
-Hide columns A B
-Hide Index column F

----------------------------------------------------

The only 'problem' is that both Description and Index
are store in the user entry table.

Hope this helps,
Sébastien
0
 
LVL 16

Expert Comment

by:sebastienm
ID: 7047328
Thank you Krolfes

Sébastien
0
 

Author Comment

by:krolfes
ID: 7047340
I just posted another question...can you help...
0
 
LVL 9

Expert Comment

by:Sar1973
ID: 13733913
Hello, I have this problem in creating drop down lists based on External data reference included in other worksheets of the same file.
Data in the list is not refreshed as well as the data (query from Access) included in the other sheet.
Why..?
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
With its various features, Office 365 can not only help you with your day-to-day business tasks, it can also do wonders for your marketing campaign.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question