How to Create a Quick Search using VB.net and MS Access

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.

What is Visual Basic’s purpose?

The third-generation programming language was created to aid developers in the creation of Windows applications. It has a programming environment that allows programmers to write code in.exe or executable files. They can also utilize it to create in-house front-end solutions for interacting with huge databases. Because the language allows for continuing changes, you can keep coding and revising your work as needed.

However, there are some limits to the Microsoft Visual Basic download. If you want to make applications that take a long time to process, this software isn’t for you. That implies you won’t be able to use VB to create games or large apps because the system’s graphic interface requires a lot of memory and space. Furthermore, the language is limited to Microsoft and does not support other operating systems.

What are the most important characteristics of Visual Basic?

Microsoft Visual Basic for Applications Download, unlike other programming languages, allows for speedier app creation. It has string processing capabilities and is compatible with C++, MFC, and F#. Multi-targeting and the Windows Presentation Framework are also supported by the system, allowing developers to create a variety of Windows apps, desktop tools, metro-style programs, and hardware drivers.

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?”.

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.

how to search record in vb.net using ms access
how to search record in vb.net using ms access

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:

'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:

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:

'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:

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:

'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:

Leave a Comment