Question How to make textbox fetch data from a table

wizard2773

New member
Joined
Jun 28, 2013
Messages
1
Programming Experience
Beginner
Hi, how does one manage to make a textbox fetch data from a table record field in ms access database after selecting a record id in a combobox and the value in the textbox must keep changing with the record id in the combobox selected, i managed to make the combobox fetch the record id's from the table field.

Here is my code:

Imports System.Data.OleDb
Module Module1
Public acsconn As New OleDb.OleDbConnection
Public acsdr As OleDbDataReader
Public strsql As String
Sub connect()
acsconn.ConnectionString = "PROVIDER=Microsoft.ACE.OLEDB.12.0; data source=|datadirectory|\FranklinMachine.accdb;"
acsconn.Open()
End Sub
End Module

Public Class frmFranklinManager
Sub fillcombo()
strsql = "select * from tblFranklin"
Dim acscmd As New OleDb.OleDbCommand
acscmd.CommandText = strsql
acscmd.Connection = acsconn
acsdr = acscmd.ExecuteReader
While (acsdr.Read())
ComboBox1.Items.Add(acsdr("FranklinName"))

End While

acscmd.Dispose()
acsdr.Close()


End Sub

PLEASE HELP!
 
You should bind the data to both controls. You then don't need any code to update the TextBox when the selection in the ComboBox changes.
command.CommandText = "SELECT ID, Name FROM SomeTable"

Dim table As New DataTable

Using reader = command.ExecuteReader()
    table.Load(reader)
End Using

With idComboBox
    .DisplayMember = "ID"
    .ValueMember = "ID"
    .DataSource = table
End With

nameTextBox.DataBindings.Add("Text", table, "Name")
 
Back
Top