Module Module1
Public connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=X:\DATA\CONTACT.mdb;Persist Security Info=True;Jet OLEDB:Database Password=4410210091"
Public Function GetTableForm(_SQL As String) As System.Data.DataTable
Dim ds As New System.Data.DataSet()
Dim conn1 As New System.Data.OleDb.OleDbConnection(connectionString)
conn1.Open()
Dim adapter1 As New System.Data.OleDb.OleDbDataAdapter(_SQL, conn1)
adapter1.Fill(ds)
Return ds.Tables(0)
End Function
End Module
Code (VB.NET)
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim dt As System.Data.DataTable = GetTableForm("SELECT Name_1 FROM tblContact_Companyslist WHERE (Com_ID like '%001%')")
Label1.Text = dt.Rows(0).Item(0).ToString()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim dt As System.Data.DataTable = GetTableForm("SELECT Name_1 FROM tblContact_Companyslist WHERE (Com_ID like '%" & TextBox1.Text & "%')")
Label1.Text = dt.Rows(0).Item(0).ToString()
End Sub
End Class
โค้ดบ้านๆคือ SELECT WHERE like แล้ว เอา Row มาใช้งานครับ(Rows(0) เผื่อมีหลาย Row ครับ )
Private Sub TextBox1_Leave(sender As Object, e As EventArgs) Handles TextBox1.Leave
Dim dt As System.Data.DataTable = GetTableForm("SELECT Name_1 FROM tblContact_Companyslist WHERE (Com_ID like '%" & TextBox1.Text & "%')")
Label1.Text = dt.Rows(0).Item(0).ToString()
End Sub
แค่ไม่แนะนำให้ใช้ SELECT WHERE หรือ SQL กับ TextChanged