Solved

populate Excel dropdown with Access data

Posted on 2002-05-29
7
643 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
  • 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
ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

 
LVL 16

Accepted Solution

by:
sebastienm earned 50 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

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Copying and pasting pictures from Excel 2 49
command prompt wont close after executing batch file 11 47
Office 2016 without internet 6 38
sumifs excel 2013 3 16
No matter the version of Windows you are using, you may have some problems with Windows Search running too slow or possibly not running at all. Before jumping into how you can solve this issue, just know there are many other viable alternative deskt…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

821 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