Results 1 to 7 of 7

Thread: [RESOLVED] Search string syntax

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2009
    Posts
    243

    Resolved [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

  2. #2
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    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.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2009
    Posts
    243

    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?

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    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.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jan 2009
    Posts
    243

    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.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jan 2009
    Posts
    243

    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
  •  



Click Here to Expand Forum to Full Width