Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Value of the value member 3

Status
Not open for further replies.

LonnieJohnson

Programmer
Apr 16, 2001
2,628
0
0
US
Can I get the value of the ValueMember that has been selected. For example...

cboFName.DataSource = DS_FNAME.Tables(0)
cboFName.ValueMember = "CUSTID"
cboFName.DisplayMember = "CUSTNAME"

If I select CUSTNAME "Jones" from the list and his corresponding CUSTID = "J1", how do I put "J1" into MyVariable?

ProDev, MS Access Applications
Visit me at ==> Contact me at ==>lonniejohnson@prodev.us

May God bless you beyond your imagination!!!
 
Code:
Dim drv as DataRowView
drv = cboFName.SelectedItem()
MyVariable = drv.Item("CUSTID")
 
RiverGuy,

You're my new hero!!!!!

ProDev, MS Access Applications
Visit me at ==> Contact me at ==>lonniejohnson@prodev.us

May God bless you beyond your imagination!!!
 
Understand, however, that this works when you bind your combobox to a datasource. It doesn't work if you just add items programattically. For example, you might have your own custom class in the .Items() collection of the combobox. To get the value, you would do something different. Take this example:

Code:
Public Class MyObject
    Private mObjectID As Integer
    Private mObjectDesc As String

    Public Property ObjectID() As Integer
        Get
            Return mObjectID
        End Get
        Set(ByVal Value As Integer)
            mObjectID = Value
        End Set
    End Property

    Public Property ObjectDesc() As String
        Get
            Return mObjectDesc
        End Get
        Set(ByVal Value As String)
            mObjectDesc = Value
        End Set
    End Property
End Class

Then, on your form load:
Code:
Dim o1 As New MyObject()
o1.ObjectID = 2
o1.ObjectDesc = "Item 2"
Dim o2 As New MyObject()
o2.ObjectID= 4
o2.ObjectDesc= "Item 4"
Dim o3 As New MyObject()
o3.ObjectID= 6
o3.ObjectDesc= "Item 6"
ComboBox1.Items.Add(o1)
ComboBox1.Items.Add(o2)
ComboBox1.Items.Add(o3)
ComboBox1.DisplayMember = "ObjectDesc"
ComboBox1.ValueMember = "ObjectID"

Then, when you want to use the value:
Code:
Dim mo As MyObject
mo = ComboBox1.SelectedItem
TextBox1.Text = mo.ObjectID
 
when the combobox is databound you can use the SelectedValue property. Assuming you have Option Explit On and the value as in your example is a string.

Code:
MessageBox.Show(CType(ComboBox1.SelectedValue, String))
 
Thanks John, this particular box is unbound and is used to search a master table for client names then populate some other text boxes with some corresponding information once a choice is made.

ProDev, MS Access Applications
Visit me at ==> Contact me at ==>lonniejohnson@prodev.us

May God bless you beyond your imagination!!!
 
John's solution should work in your case. It appears you are setting the datasource. I had just got in the habit of doing it they way I noted above.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top