Re: Combobox value to be displayed in text box


I have a table in an Access DB as follows

id,rofl
1,one
2,two
3,three
4,four
5,five

I have a vb form with a combo (combobox1) and a textbox (textbox1)

What I need to do is when I make a selection in the combo (rofl field in db) the textbox will display the corresponding value in the id field of the table

eg select "three" textbox shows: 3

So far I have the code below but every selection I make returns the value 5, any idea how I can fix the code and what I am doing wrong Im fairly sure that the error surround the bolded text. Be easy on me this my first post

VB Code: Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim cn As ADODB.Connection Dim rs As ADODB.Recordset cn = New ADODB.Connection rs = New ADODB.Recordset cn.ConnectionString = "Provider=MSDASQL.1;Persist Security Info=False;Extended Properties='DSN=MS Access Database;DBQ=D:\Documents and Settings\James\My Documents\db1.mdb;DefaultDir=D:\Documents and Settings\James\My Documents;DriverId=25;FIL=MS Access;MaxBufferSize=2048;PageTimeout=5;UID=admin;';Initial Catalog=D:\Documents and Settings\James\My Documents\db1" cn.Open() rs.Open("select * from mytable", cn, ADODB.CursorTypeEnum.adOpenKeyset, ADODB.LockTypeEnum.adLockPessimistic) rs.MoveFirst()[B] Do Until rs.EOF = True ComboBox1.ValueMember = rs.Fields("id").Value ComboBox1.Items.Add(rs.Fields("rofl").Value) rs.MoveNext() Loop[/B] rs.Close() cn.Close() rs = Nothing cn = Nothing End Sub Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged TextBox1.Text = ComboBox1.ValueMember End SubEnd Class
Posted On: Saturday 3rd of November 2012 12:49:51 PM Total Views:  196
View Complete with Replies