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

0
5266

When you are about to create any application you always ask yourself “what are the basic features your 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 “How to Create a Quick Search using Visual basic.Net”. 

 

First download the database to be used for this lesson. Click this—>books

Next we will now create a Visual basic Project named “Bookfinder” then, extract the download database and put it inside the Bin folder.

After creating a project in visual Basic. Let’s now design the form. Follow the Image below on the form looks like.

bookfinder

 

Next, Let’s add functionality to our application by adding some code to our objects.

 

First we will add declaration under public class: and here’s the code:

[vbnet]

‘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

[/vbnet]

Next, Double click the form, and on the form load add the following code:

[vbnet]

con.ConnectionString = “Provider=Microsoft.ACE.OLEDB.12.0;Data Source=” & Application.StartupPath & “\books.accdb”

btntxtbox.Enabled = False
txtquick.Visible = False
[/vbnet]

 

Double the “Search” button and add the following code:

[vbnet] ‘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()
[/vbnet]

Next double click the “Use Quick Search” button. And ad the following code:

[vbnet]

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

[/vbnet]

Then, double click the textbox under “Quick Search” Label make sure the you will be redirected to “Texhchanged Event“. This allows you to perform quick search because every time you type on the textbox provided it will automatically gives you the results on the datagridiview based on the keyword inputted by the user.

And here’s add the following code:

 

[vbnet] ‘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()

[/vbnet]

and you can now press the “F5” on your keyboard or simply run your project.

You can download the full source code here.

 

Readers might read also:

 

 

Facebook Comments

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.