OLeDB
to access OLeDB
libraries.
imports System.Data.OleDb;
Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\dbproducts.accdb") Dim cmd As OleDbCommand Dim da As OleDbDataAdapter Dim dt As DataTable Dim sql As String Dim maxrow As Integer
Private Sub load_cbo(sql As String, cbo As ComboBox) Try con.Open() cmd = New OleDb.OleDbCommand da = New OleDb.OleDbDataAdapter dt = New DataTable With cmd .Connection = con .CommandText = sql End With da.SelectCommand = cmd da.Fill(dt) cbo.DataSource = dt cbo.DisplayMember = "Product" cbo.ValueMember = "ProductID" Catch ex As Exception MsgBox(ex.Message) Finally con.Close() da.Dispose() End Try End Sub
Private Function get_maxrow(sql) Try con.Open() cmd = New OleDb.OleDbCommand da = New OleDb.OleDbDataAdapter dt = New DataTable With cmd .Connection = con .CommandText = sql End With da.SelectCommand = cmd da.Fill(dt) maxrow = dt.Rows.Count Catch ex As Exception MsgBox(ex.Message) Finally con.Close() da.Dispose() End Try Return maxrow End Function
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load sql = "Select * From tblproducts" load_cbo(sql, cboProduct) End Sub
Private Sub btnFind_Click(sender As Object, e As EventArgs) Handles btnFind.Click sql = "Select * From tblproducts Where ProductID = " & cboProduct.SelectedValue & "" maxrow = get_maxrow(sql) If maxrow > 0 Then With dt.Rows(0) txtProductID.Text = .Item("ProductID") txtProductName.Text = .Item("Product") txtDescription.Text = .Item("Description") txtPrice.Text = .Item("Price") txtCategory.Text = .Item("Category") End With End If End Sub