tags:

views:

2159

answers:

6

This should probably be pretty simple but my Google-Fu is as yet unable to find an answer. I simply want to create a dropdown list in Access so that upon selection I can perform some action based on the value of the selection. For instance, I have a list of people and I would like to populate the combo box so that their names appear in the list but the "value" is set to their ID (the primary key).

A: 

You will need to hook into the onchange event for the dropdown list.

and from MSDN

Chris Ballance
Sorry, my question was ill-formed. My problem wasn't on the coding side, it was understanding the semantics of the combobox. In other languages/frameworks I would just bind a bunch of key/value pairs to some list control but I couldn't figure out how to do it using the Access 'IDE'.
Rodrick Chapman
A: 

How have you set the properties for your combo box?

Perhaps you could try setting (assuming you are pulling data from Table1 with fields ID and Field1

  • Row Source: SELECT [Table1].[ID], [Table1].[Field1] FROM Table1;
  • Row Source Type: Table/Query
  • Bound Column: 1
  • Column Count: 2
  • Column Widths: 0", 1"

and then hook into the onchange event as Chris Ballance suggests. The value property of the combo box is ID; the text will be what is in Field1.

Azim
+3  A: 

It sounds like you might be asking how to display something in the dropdown other than the ID while keeping the ID as the returned data from the dropdown. If that's the case set the Bound Column to the ID field (usually 1) and (assuming the name field is next) set the Column Count to be 2 and the Column Widths to be 0";1" or 0";[whatever width you need].

Ah, thank you. I had just figured that out right before I saw your answer. I'll delete my answer and give you the credit.
Rodrick Chapman
A: 

OK, I figured it out even though it was a bit counter-intuitive. An Access Combobox can have as many values as you want (instead of just one key on value). By default all of the values are are shown in the list so you need to hide certain columns by setting their widths to 0. That is done via the ColumnsWidths property in the property pane. ColumnWidths takes a comma separated list of values which corresponds to the order of the columns in the list. I hope this helps someone.

Rodrick Chapman
A: 

But, how do you get the field 1 info to appear in the box rather than the ID?

A: 

the setting you noted does not solve the actual problem of bounding to an ID and displaying the text field in the drop down list, what you showed display the text field while selecting only ... How can we display the actual text instead of the id once the selection has been made. We need this when you want to store IDs in the database table and display meaningful text (like department name instead of ID) in the drop down field.

Replies will be appreciated

Omar A.
Which answer are you saying is wrong? The one from @Scott Konkle that was accepted as the answer is completely correct.
David-W-Fenton