Search and Display the contents of Access database inside a VB Form

Contributor
Submitted by CAAadmin on Wed, 09/07/2016 - 05:24
vb net access database

You can display the contents of Access database inside a VB Form using DataGridView controls (from the tool box)

Steps

Add a textbox, a button and a data grid view

Double click on the form and add below codes (This code will search for textbox1.text value in database and display all tables inside datagrid view). Put the database (database.accdb) inside debug folder

In this example, assume there is a column called F2 inside table1 of database.accdb

When you type something inside Textbox1, if the types characters are matching with any cells of F2 , entire rows will be displayed inside DataGridView

Imports System.Data.OleDb

Dim con As New OleDbConnection

     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        con.ConnectionString = "provider=Microsoft.ACE.OLEDB.12.0;Data Source=..\\Database\database.accdb;"

          End Sub

 

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        con.Open()

        Dim dt1 As New DataTable

        Dim ds1 As New DataSet

        ds1.Tables.Add(dt1)

        Dim da1 As New OleDbDataAdapter

        da1 = New OleDbDataAdapter("select * from Table1 where F2 like '%" & TextBox1.Text & "%'", con)

        da1.Fill(dt1)

        DataGridView1.DataSource = dt1.DefaultView

        con.Close()

    End Sub

 

These are examples only, you can suggest any improvements or report any errors via comment box for our discussion forum