Storing and then retrieving ValueMember info from a list or combo box

T

travlintom

Dim intCurID As Integer *** outside of code and available to all
***

Dim strConnectionString As String = _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\Databases\ClientAppDB.mdb;"
Dim objConnection As New OleDbConnection(strConnectionString)
Dim strSQl As String = "SELECT CLIENTID, FIRSTNAME, LASTNAME
FROM CLIENTS ORDER BY LASTNAME"
Dim objCommand As New OleDbCommand(strSQl, objConnection)
Dim objDataReader As OleDbDataReader


Try
objConnection.Open()
objDataReader = objCommand.ExecuteReader *** is this
correct ***

While objDataReader.Read()
Me.ListBox1.Items().Add(objDataReader.Item(2) & ", "
& objDataReader.Item(1))
Me.ListBox1.ValueMember = objDataReader.Item(0)
End While

*** All this code works perfectly and the ValueMembers show as
correct when looping throught the data reader ***


Private Sub ListBox1_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
ListBox1.SelectedIndexChanged

intCurID = Me.ListBox1.SelectedValue
End Sub

*** Here I want get the current selected Value but ***
*** all it show is zero ***
*** Am I going about this the wrong way? I want to use ***
*** code and not the Properties on the form due to a ***
*** calculated SQL statment I use for the listbox ***

*** Thanks for any help ***
 
C

Cor Ligthert [MVP]

Tom,

The best thing you can do is to put in top of your program or in the VS
properties

Option Strict On

Probably you get the result by
intCurID = Cint(Me.ListBox1.SelectedValue)

I hope this helps,

Cor
 
T

travlintom

Cor..........Thanks for your help. When I put Option strict on I got a
ton of errors pertaining to the data objects. I changed the option to
Explicit. Your last line of code did not work. It still showed the
selected value as zero. Thanks anyway
Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top