Solved

populate Excel dropdown with Access data

Posted on 2002-05-29
7
639 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

How many times recently have you prepared a presentation or emailed a document to a client and you have found that they have older versions of MS Office and they can not open the file you have prepared.  Although most visitors to this site are exper…
Outlook Free & Paid Tools
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

759 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now