|
-
May 1st, 2009, 08:23 AM
#1
Thread Starter
Addicted Member
[RESOLVED] Search string syntax
I have a TextBox for the user to input a search string. My button click code returns thr error 'No value given for one or more Parameters. I have tried various ways to write a SELECT string. Help appreciated.
Code:
Private Sub Button36_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button36.Click
'Search
Try
'Clear the Datatable called dt
dt.Clear()
'SELECT conditions
Dim SearchStr As String = "'%" & Me.TextBox18.Text & "%'"
da = New OleDb.OleDbDataAdapter("SELECT * FROM [sheet1$] WHERE Surname LIKE SearchStr AND Member LIKE('%Ply.Member%') ORDER BY Surname", con)
da.Fill(dt)
'Bind the DataGrid to the Table
DataGridView1.DataSource = dt
Catch ex As Exception
MsgBox(ex.Message)
Finally
con.Close()
End Try
End Sub
Last edited by Kochanski; May 1st, 2009 at 10:47 AM.
Reason: typing error
-
May 1st, 2009, 08:28 AM
#2
Re: Search string syntax
You appear to be attempting to intermingle variables defined locally such as "SearchStr" with a SQL query - that won't work.
You need to pass things like SearchStr into the SQL query, either using Parameters or amending the literal text you are passing in.
-
May 1st, 2009, 08:32 AM
#3
Thread Starter
Addicted Member
Re: Search string syntax
Thanks. I'm sure you are right, but I can't progress this because I don't know how to amend the SELECT syntax. How do I get from a user input to a SELECT statement?
-
May 1st, 2009, 08:37 AM
#4
Re: Search string syntax
But you do.... you did it here:
Dim SearchStr As String = "'%" & Me.TextBox18.Text & "%'"
...
"SELECT * FROM [sheet1$] WHERE Surname LIKE " & SearchStr & " AND Member LIKE('%Ply.Member%') ORDER BY Surname"
-tg
-
May 1st, 2009, 08:39 AM
#5
Re: Search string syntax
Well it looks like you have three user defined parameters in there : [sheet1$], '%Ply.Member%' and Searchstring - is that right or are the first two of them actually objects in the database?
If so then you set the query to just have placeholders initially, so it would become something like
"SELECT * FROM ? WHERE Surname LIKE ? AND Member LIKE(?) ORDER BY Surname"
and add Parameters to represent the placeholders.
Alternatively as techgnome illustrated, if you have variables containing the searchstring you can just append the variables into the SQL statement. You kinda did that in your example except you had the variable name inside the string so it wouldn't evaluate to the contents, to get that working you do what tg showed.
Unfortunately I don't know where you are getting the other values that you want to use in the query if they are variables - can you shed more light on what [sheet1$] and Ply.Member are?
NB - using parameters is the preferred way of doing this - see here for details.
Last edited by keystone_paul; May 1st, 2009 at 08:44 AM.
-
May 1st, 2009, 08:48 AM
#6
Thread Starter
Addicted Member
Re: Search string syntax
Thanks tg - just those missing " & in the statement. The code works fine. I'm guessing I will have to search all fields by declaring each one in turn followed by a LIKE statement. Is there any way to search all fields without repeating the Parameters?
Code:
Dim SearchStr As String = "'%" & Me.TextBox18.Text & "%'"
da = New OleDb.OleDbDataAdapter("SELECT * FROM [sheet1$] WHERE Surname LIKE " & SearchStr & " OR Forname LIKE " & SearchStr & " ORDER BY Surname", con)
Last edited by Kochanski; May 1st, 2009 at 10:46 AM.
-
May 1st, 2009, 10:45 AM
#7
Thread Starter
Addicted Member
Re: Search string syntax
Only if I learn to spell parameters, I guess.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|