?
Solved

populate Excel dropdown with Access data

Posted on 2002-05-29
7
Medium Priority
?
662 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
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
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

Office 365 Training for IT Pros

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Cancel future meetings from user mailboxes in Office 365 using Remove-CalendarEvents
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

777 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