Results 1 to 6 of 6

Thread: [RESOLVED] trouble with LIKE operator

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2009
    Location
    B.C. Canada
    Posts
    206

    Resolved [RESOLVED] trouble with LIKE operator

    i am new to the LIKE operator. i am setting up a search in an access database so the user can search a partial string, eg search for "White" and find all records with the word white as part of the field contents.
    this works when i make a query inside access but i cannot duplicate it in VB6 when connected to the same database table. i get an empty recordset
    what am i doing wrong?
    Code:
       
    Private Sub Form_Load()
    Set CNSKUList = New ADODB.Connection
    CNSKUList.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    "Data Source=" & WorkFlowDatabasePath & ";" & "user id=admin;password=;"
    CNSKUList.Open
    Set RstSKUList = New ADODB.Recordset
    end sub
    
    Private Sub SearchButton_Click()
     RstSKUList.Open "select * from SKULIST WHERE Description LIKE ' * White * '  ", CNbatchtable, adOpenKeyset, adLockOptimistic, adCmdTableDirect
                   Set SKUListMSHFlexGrid.DataSource = RstSKUList
       RstSKUList.Close
    end sub
    Last edited by daveinarmstrong; Oct 25th, 2011 at 12:47 PM.

  2. #2
    Frenzied Member
    Join Date
    May 2006
    Location
    some place in the cloud
    Posts
    1,886

    Re: trouble with LIKE operator

    Try with no spaces
    Code:
    LIKE '*White*' ",
    and, try with % instead of *
    Code:
    LIKE '%White%' ",
    JG


    ... If your problem is fixed don't forget to mark your threads as resolved using the Thread Tools menu ...

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Dec 2009
    Location
    B.C. Canada
    Posts
    206

    Re: trouble with LIKE operator

    Thanks, this works.
    what if i was using a variable for the search string? i am getting no data using a variable.

    Code:
    dim MyVariable as string
    MyVariable = "White"
    
    LIKE '%MyVariable%' ",

  4. #4
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    Re: trouble with LIKE operator

    "LIKE '%" & MyVariable & "%' "

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: trouble with LIKE operator

    Just like other times you want to append a variable to a string, use the variable rather than a piece of text which happens to be the same as the variable name.

    For more information, see the article How can I put the value of a variable/control into my SQL statement? from our Database Development FAQs/Tutorials (at the top of this forum)


    edit: I was slow!

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Dec 2009
    Location
    B.C. Canada
    Posts
    206

    Re: trouble with LIKE operator

    this works, thanks.
    Code:
    LIKE '%" & MyVar & "%'

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