GWILSON1981

Hi

I have a table similar to this:

Type Name

1 Bob

2 Claire

3 Peter

Type is the primary key.

On a form I have a combo box that I want the user to pick a name. The value stored though needs to be the primary key, here's my question: In access you can create combo boxes that display both columns within a combo but when an item is selected it stores the first column only.

How do I replicate this using Visual Basic 2005 - how do you guys do this If I populate the combo with the primary key only, it's difficult to use since the user doesn't know which [Name] belongs to which [type].

Your help is appreciated!



Re: Visual Basic Express Edition Combobox databinding -- almost there!

ahmedilyas

not sure I follow. how are you binding it

did you set the ValueMember property to the Type field

basically when you fill the dataset/datatable, you set the fields to the DisplayMember and ValueMember...

Me.theComboBox.DisplayMember = "Name"

Me.theComboBox.ValueMember = "Type"






Re: Visual Basic Express Edition Combobox databinding -- almost there!

GWILSON1981

Thank you very much for your help :-)

This is exactly what I'm looking for, the only last question I have is this. Once I've set the DisplayMember and ValueMember, how do I retrieve the value if I use e.g. txtAccountName.text then this returns the display member and not the value member.

Thanks again.





Re: Visual Basic Express Edition Combobox databinding -- almost there!

ahmedilyas

no worries! :-)

to get the value member try... SelectedValue:

Me.ComboBox1.SelectedValue






Re: Visual Basic Express Edition Combobox databinding -- almost there!

GWILSON1981

Perfect!!!



Re: Visual Basic Express Edition Combobox databinding -- almost there!

GWILSON1981

One other question using this method please :-)

When I open the form the combo box has the value of the first item in the list selected. Ideally, I want the initial value to be null. I could add a null value to the "look-up" list but I don't want to! The user must make a selection but I'd rather it didnt default in this way.


Any ideas





Re: Visual Basic Express Edition Combobox databinding -- almost there!

ahmedilyas

you can change the SelectedIndex to -1:

Me.theComboBox.SelectedIndex = -1

place this on say the form load event or after you bound the data