Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to make a search bar. So far this code works if I type the exact words of the information I want to search, but what I want to do is to at least detect 8-10 characters from what I typed in the search bar and the results would show.

Also.. I am having problems when searching for number data types. It won't let me search information with numbers in it. Only texts..

Any idea why?

VB
connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\lppUni.mdb;"
       Dim ds As New DataSet
       Dim sql As String = "Select * from LPPUN where studID = '" & TextBox1.Text & "' OR studName = '" & TextBox1.Text & "' OR studCourse = '" & TextBox1.Text & "'"
       Dim dt As DataTable
       cnn = New OleDbConnection(connectionString)

       Try
           cnn.Open()
           adptr = New OleDbDataAdapter(sql, cnn)
           adptr.Fill(ds)
           adptr.Dispose()
           cnn.Close()

           dt = ds.Tables(0)
           studformGridView.DataSource = dt
       Catch ex As Exception
           MsgBox(ex.ToString)
       End Try

       studformGridView.Visible = True
   End Sub
Posted
Updated 18-Mar-13 2:22am
v2

Use Like in place of =

"Select * from LPPUN where studID Like '%" & TextBox1.Text +"%' OR studName Like '%" & TextBox1.Text + "%' OR studCourse Like '%" & TextBox1.Text + "%'"

and your search will return with matching characther.
 
Share this answer
 
Comments
Semeki 18-Mar-13 11:36am    
Oh it doesn't return anything unless you type the exact word that you are searching for :(
As with the prev Solution, use "like" but also change the "+" with "&"

e.g.

"Select * from LPPUN where studID Like '%" & TextBox1.Text & "%' OR studName Like '%" & TextBox1.Text & "%' OR studCourse Like '%" & TextBox1.Text & "%'"

Also, watch out for intentional / non-intentional SQL injection with your textbox values

:-)
 
Share this answer
 
Comments
Semeki 18-Mar-13 12:53pm    
Thank you very much, it's working now! But I'm wondering if we can do the same thing if the data type is in Integer? Because it doesn't seem to work.
bigcdh 18-Mar-13 12:56pm    
should be exactly the same... whats the error you get?
Semeki 19-Mar-13 7:06am    
There is no error but it the data grid view doesn't show any data at all.
bigcdh 19-Mar-13 7:10am    
Hmm, well shouldnt be any reason why it wouldnt work that I can see.

Try taking the try / catch out and see if it throws an error which may give you more of an indication.

Also, just out of interest, you dont need to use cnn.Open() or cnn.Close() as when filling an adapter with a dataset this is already taken care of. Just a couple less lines of code :-)
Semeki 19-Mar-13 23:13pm    
Okay it's working now! Thank you very much for helping me! :D

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900