Search code examples
vb.netms-accesslistboxsearchbarsearch-box

Searching Algorithm using listbox


I am trying to make a search box/ bar by using access Database. i want the code to search for the record and then then remove/hide the record from listbox when i search for another record. My programs belows searches for the Record and displays it, however there are few problems.

  1. does not remove/ hide the record when searching again.
  2. does not respond if the record isn't in database or if search box is empty.

 Dim con As New OleDbConnection("Provider=Microsoft.jet.oledb.4.0;data source=C:\Users\jacob\Desktop\MS Office\project.mdb")
    Dim cmd As OleDbCommand = New OleDbCommand("SELECT * FROM tblProduct WHERE productID LIKE '" & txtSearch_Bar.Text & "'", con)
    con.Open()
    Dim sdr As OleDbDataReader = cmd.ExecuteReader()

    While (sdr.Read())

        lstbSearchResult.Items.Add(sdr("ProductID"))

    End While

i would also like to make this into a dynamic search bar.


Solution

  • I think your first requirement is addressed in the comments. For your second requirement, add the following before your While loop.

    If Not sdr.HasRows Then
        MessageBox.Show("No Matches")
        Exit Sub
    End If