0

Dear Sir
I have access data table and vb . project .access data table include ID ,word and description Colum v b project include three text box and one list box. If I search any word in text box through word Colum .I want show description Colum next text box how can do it.
Please help me.
Thank you
Wansa

Imports System.Data
Imports System.Data.OleDb

Public Class Form1
    WithEvents bs As New BindingSource
    Dim tblcontactconnection As New OleDb.OleDbConnection
    Dim provider As String
    Dim dbtblcontact As String
    Dim dstblcontact As New DataSet
    Dim datblcontact As OleDb.OleDbDataAdapter
    Dim SqlQuery As String
    Dim NumberofRows As Integer
    Dim NumberRow As Integer = 0
    Dim dt As DataTable

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        provider = "Provider=Microsoft.Ace.Oledb.12.0;"
        dbtblcontact = "Data Source=C:\Users\Home\Desktop\english.accdb"
        tblcontactconnection.ConnectionString = provider & dbtblcontact
        tblcontactconnection.Open()
        'access data table name is game
        SqlQuery = "SELECT * FROM wordlist"
        datblcontact = New OleDb.OleDbDataAdapter(SqlQuery, tblcontactconnection)
        datblcontact.Fill(dstblcontact, "wordlist")
        tblcontactconnection.Close()
        Me.bs.DataSource = dstblcontact.Tables("wordlist")
        'access data table searching coloum is word
        Me.ListBox1.DisplayMember = "word"
        Me.ListBox1.DataSource = Me.bs

    End Sub
    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        Dim dt As DataTable = CType(bs.DataSource, DataTable)
        dt.DefaultView.RowFilter = String.Format("word like '{0}%'", TextBox1.Text)
        If dt.DefaultView.Count > 0 Then
            For Each item As DataRowView In dt.DefaultView

TextBox2=

            Next
        End If
    End Sub
End Class
Attachments 222.png 13.1 KB
2
Contributors
2
Replies
25
Views
6 Months
Discussion Span
Last Post by xrj
0

I've felt free to modify a bit your code to what I thought was required:

Imports System.Data
Imports System.Data.OleDb
Imports System.Linq

Public Class TableDescription

    Dim conn As New OleDb.OleDbConnection
    Dim ds As New DataSet
    Dim tblName As String = "Contacts"
    Dim tbl As DataTable
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ConnectToAccess()
    End Sub
    Public Sub ConnectToAccess()
        Dim conn As OleDbConnection = Nothing
        Try
            conn = New OleDbConnection
            conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" +
            "Data Source=C:\Users\Public\database1.accdb;" _
            + "Persist Security Info=False;"
            Try
                conn.Open()
            Catch ex As Exception
                MessageBox.Show("Failed to connect to data source")
            End Try
            Dim SqlQuery As String = "SELECT * FROM " + tblName
            Dim datblcontact As OleDb.OleDbDataAdapter
            datblcontact = New OleDb.OleDbDataAdapter(SqlQuery, conn)
            datblcontact.Fill(ds, tblName)
            Me.ListBox1.DataSource = ds.Tables(tblName)
            Me.ListBox1.DisplayMember = "Name"
            ' save the dataset's ds.table(0):
            tbl = ds.Tables(tblName)
        Catch ex As Exception
        Finally
            If conn IsNot Nothing Then
                conn.Close()
            End If
        End Try
    End Sub

    Private Sub tbID_TextChanged(sender As System.Object, e As System.EventArgs) Handles tbID.TextChanged
        Try
            Dim ID1 As Int32 = Int32.Parse(tbID.Text)
            ' select from the saved table 'tbl' those
            ' row(s) whose row.ID = ID1. Furthermore, select
            ' the field 'name': '
            Dim query = tbl.AsEnumerable().Select( _
                Function(row As DataRow) New With
                { _
                    .ID = ID1, _
                    .Name = row.Field(Of String)("Name") _
                })
            Dim IDrow = query(0) ' first selection row '
            ' assign the field 'name' from the first select row: '
            tbName.Text = IDrow.Name
        Catch ex As Exception
            tbName.Text = "Please, enter a valid ID."
        End Try
    End Sub
End Class
0

More concisely, we may write:

    Private Sub tbID_TextChanged(sender As System.Object, e As System.EventArgs) Handles tbID.TextChanged
        Try
            Dim ID1 As Int32 = Int32.Parse(tbID.Text)
            Dim query = From row As DataRow In tbl.Rows Where row.Item("ID") = ID1
            tbName.Text = query(0).Item("Name")
        Catch ex As Exception
            tbName.Text = "Please, enter a valid ID."
        End Try
    End Sub
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.