Results 1 to 2 of 2

Thread: SQL query with parameters and wildcards

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2006
    Posts
    1

    Question SQL query with parameters and wildcards

    Hi,
    what I want to do is to run a query like this one:
    Code:
    SELECT RadID,Leverantor FROM InkopsOrder WHERE Leverantor LIKE '%kompaniet%'
    which works fine, but I want the '%kompaniet%' part to be a parameter, like this:
    Code:
    "SELECT RadID,Leverantor FROM InkopsOrder WHERE Leverantor LIKE ?"
    And I can't get it to work out! The problem is I don't get anything in my recordset when using the second query.
    Here's what my code looks like:

    Code:
    Set comm.ActiveConnection = conn
    comm.CommandText = _
        "SELECT RadID,Leverantor FROM InkopsOrder WHERE Leverantor LIKE ?"
    sokstrang = "'%" & InputBox("Söksträng?") & "%'"
    comm.Parameters(0) = sokstrang
    rec.Open comm
    ws.[a1].CopyFromRecordset rec
    'ws.[d1].Cells(1) = comm.Parameters(0)
    The
    'ws.[d1].Cells(1) = comm.Parameters(0)
    is there so I can check that the parameter actually has the value '%kompaniet%', which it has.

    Now, i cant use
    Code:
    "SELECT RadID,Leverantor FROM InkopsOrder WHERE Leverantor LIKE '%?%'"
    Or anything like that since It doesnt seem to recognize ? as a parameter when I do.

    PLEASE help me out on this one :P
    thanks.
    Reply With Quote

  2. #2
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: SQL query with parameters and wildcards

    Code:
    dim strSql as string
    
    sokstrang = "'%" & InputBox("Söksträng?") & "%'"
    strSql = "SELECT RadID,Leverantor FROM InkopsOrder WHERE Leverantor LIKE " & sokstrang
    
    '---- shouldn't this be a recordset not a command??
    Set comm.ActiveConnection = conn
    comm.CommandText = strSql
    rec.Open comm
    ws.[a1].CopyFromRecordset rec

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

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