How to create a Quick Search using VB.net and MS Access
This tutorial about Search Code in VB.Net Using Access will help you create a quick search using vb.net and MS Access database.
Sample Scenario
When you are about to create any application you always ask yourself “what are the basic features you’re going to include in your system?”.
Watch the video here to see the running application.
Well, the most common features that are always included in the system is that the user can Search a record or a list of records from the database using the system.
For instance, you going to create a Library System you must have a search option for your user.
So in this tutorial, I will show you the application of Search Code in VB.Net Using Access in “How to Create a Quick Search using Visual basic.Net”.
This tutorial can be used in the next topic on How to Search Data in the DataGridView Using a TextBox in VB.Net.
Steps how to search record in vb.net using ms access
Step 1: First, download the database to be used for this lesson. Click this—>books
Step 2: Next, we will now create a Visual basic Project named “Bookfinder” then, extract the download database and put it inside the Bin folder.
Step 3: After creating a project in Visual Basic. Let’s now design the form. Follow the Image below on the form looks like.

Step 4: Next, Let’s add functionality to our application by adding some code to our objects.
Step 5: First, we will add declaration under public class: and here’s the code:
1 2 3 4 5 6 |
'this OleDbDataAdapter serves as bridge to our connection and SQL statements Dim da As New OleDb.OleDbDataAdapter Dim con As New OleDb.OleDbConnection 'this is a temporary or imaginary tables in the memory of computer same with the datasets Dim dt As New DataTable Dim sql As String |
Step 6: Next, Double click the form, and on the form, load adds the following code:
1 2 3 4 |
con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\books.accdb" btntxtbox.Enabled = False txtquick.Visible = False |
Step 7: Double the “Search” button and add the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
'we set her our SQL statements sql = "Select * from book_profile where title='" & txttitle.Text & "'" & _ " OR author= '" & txtauthor.Text & "' OR Dewey_no='" & txtdewey.Text & "' OR subject='" & txtsubj.Text & "'" Try 'open the connection con.Open() 'bind the SQL and the connection through OleDBDataAdaoter and stored to da da = New OleDb.OleDbDataAdapter(sql, con) 'and whatever the value of da will be fill into dt our imaginary data table da.Fill(dt) 'get the datasource of datagridview from our data table dtgresult.DataSource = dt Catch ex As Exception 'will throw an error if something went wrong. MsgBox(ex.Message, MsgBoxStyle.Information) End Try 'close connection con.Close() |
Step 8: Next double click the “Use Quick Search” button. And ad the following code:
1 2 3 4 5 6 7 8 9 10 11 |
txtquick.Visible = True btnquick.Enabled = False btntxtbox.Enabled = True txttitle.Enabled = False txtauthor.Enabled = False txtdewey.Enabled = False txtsubj.Enabled = False txttitle.BackColor = Color.Aqua txtauthor.BackColor = Color.Aqua txtdewey.BackColor = Color.Aqua txtsubj.BackColor = Color.Aqua |
Step 9: Then, double click the textbox under “Quick Search” Label make sure you will be redirected to “Texhchanged Event“.
This allows you to perform a quick search because every time you type on the textbox provided it will automatically give you the results on the datagridiview based on the keyword inputted by the user.
And here’s add the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
'we set her our SQL statements sql = "Select * from book_profile where title like '%" & txtquick.Text & "%' or author LIKE '%" & txtquick.Text & "%'" dt = New DataTable Try 'open the connection con.Open() 'bind the SQL and the connection through OleDBDataAdaoter and stored to da da = New OleDb.OleDbDataAdapter(sql, con) 'and whatever the value of da will be fill into dt our imaginary data table da.Fill(dt) 'get the datasource of datagridview from our data table dtgresult.DataSource = dt Catch ex As Exception 'will throw an error if something went wrong. MsgBox(ex.Message, MsgBoxStyle.Information) End Try 'close connection con.Close() |
Step 9: Finally, you can now press the “F5” on your keyboard or simply run your project.
Download the Search Code in VB.Net Using Access below
You can download the full source code here.
This article can answer most of the queries found online about how to add search code in vb.net application.
Readers might read also: