Book Finder Using Quick Search in Visual Basic.Net

This tutorial is a continuation of “Book Finder Using Visual Basic.Net and MS Access 2007”. At this time, we’re going to learn how to put a Quick search. By using a quick search, we can save more time by specifying the title of a specific book, author, Dewey number and subject. To start with this tutorial, add a new textbox and name it as “txtquick” and a label, and change the text property to “Quick Search”. Then add two buttons and the first one name it as “btnquick” and change the text property into “Use Quick Search” and the second one name it as “btntxtbox”, and change the text property to “Use Text Box Filter”. And the user interface after designing will look like as shown below. This time, we are now going to add functionality to our program. To do this double click the main form. And the following code below our “con.ConnectionString”. This code will only disable the “Use Quick Search” button and hide the “txtquick” textbox.
  1. btntxtbox.Enabled = False
  2. txtquick.Visible = False
Then double click the “btnquick” button and add the following code: The purpose of this code it will on show the “txtquick” and disable other textbox and change the back color into aqua color.
  1. txtquick.Visible = True
  2. btnquick.Enabled = False
  3. btntxtbox.Enabled = True
  4. txttitle.Enabled = False
  5. txtauthor.Enabled = False
  6. txtdewey.Enabled = False
  7. txtsubj.Enabled = False
  8. txttitle.BackColor = Color.Aqua
  9. txtauthor.BackColor = Color.Aqua
  10. txtdewey.BackColor = Color.Aqua
  11. txtsubj.BackColor = Color.Aqua
And it looks like as shown below. Then to do the opposite, double click the “Use Text box Filter”, and add the following code:
  1.  
  2. txtquick.Visible = False
  3. btnquick.Enabled = True
  4. btntxtbox.Enabled = False
  5. txttitle.Enabled = True
  6. txtauthor.Enabled = True
  7. txtdewey.Enabled = True
  8. txtsubj.Enabled = True
  9. txttitle.BackColor = Color.Lime
  10. txtauthor.BackColor = Color.Lime
  11. txtdewey.BackColor = Color.Lime
  12. txtsubj.BackColor = Color.Lime
And it looks like as shown below: And finally double click the “txtquick” textbox and add the following code: When the text is changed, it will automatically populate the datagridview and the user can view a result easily.
  1. Private Sub txtquick_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtquick.TextChanged
  2. 'we set her our SQL statements
  3. sql = "Select * from book_profile where title like '%" & txtquick.Text & "%' or author LIKE '%" & txtquick.Text & "%'"
  4.  
  5. dt = New DataTable
  6. Try
  7. 'open the connection
  8. con.Open()
  9. 'bind the SQL and the connection through OleDBDataAdaoter and stored to da
  10. da = New OleDb.OleDbDataAdapter(sql, con)
  11. 'and whatever the value of da will be fill into dt our imaginary data table
  12. da.Fill(dt)
  13. 'get the datasource of datagridview from our data table
  14. dtgresult.DataSource = dt
  15.  
  16. Catch ex As Exception
  17. 'will throw an error if something went wrong.
  18. MsgBox(ex.Message, MsgBoxStyle.Information)
  19. End Try
  20. 'close connection
  21. con.Close()
  22. End Sub
You can now press “F5” to test your program.

Add new comment