Results 1 to 17 of 17

Thread: Search with Like

  1. #1
    Addicted Member
    Join Date
    Dec 11
    Posts
    246

    Search with Like

    Hi

    I have written this statement but it is not working . i have item code like diA12thd , dabd12th so on . I am using MS-Access & i have written this statement in Visual Basic 6
    esql = "SELECT * FROM m_item WHERE icode like '12%'"

    Openrstitem (esql)

    Thanks

  2. #2
    PowerPoster
    Join Date
    Feb 12
    Location
    West Virginia
    Posts
    4,978

    Re: Search with Like

    What is Openrsitem ?

    What do you mean when you say it is not working, Are you getting a result? An error? Did it kill the cat?

  3. #3
    PowerPoster
    Join Date
    Jul 06
    Location
    Maldon, Essex. UK
    Posts
    5,387

    Re: Search with Like

    I'm guessing no items were returned. You need a wild character at the front and back:
    Code:
    esql = "SELECT * FROM m_item WHERE icode like '%12%'"

  4. #4
    Addicted Member
    Join Date
    Dec 11
    Posts
    246

    Re: Search with Like

    Hi

    Openrstitem is

    strsql = "select * from m_itemmaster order by icode"
    Openrstitem (strsql)

    Thanks

  5. #5
    Addicted Member
    Join Date
    Dec 11
    Posts
    246

    Re: Search with Like

    Hi Doogle


    when i am writing this with variable it's not working , what is wrong in this.

    strsql = "SELECT * FROM m_itemmaster WHERE icode LIKE '%'" & Trim(LCase(strsearch)) & "'%'"

    Your statement is working

    Thanks

  6. #6
    PowerPoster
    Join Date
    Feb 12
    Location
    West Virginia
    Posts
    4,978

    Re: Search with Like

    That is because you placed single quotes around the % signs which is not the same
    You would be using
    esql = "SELECT * FROM m_item WHERE icode like '%'12'%'"
    instead of
    esql = "SELECT * FROM m_item WHERE icode like '%12%'"

    Hi

    Openrstitem is

    strsql = "select * from m_itemmaster order by icode"
    Openrstitem (strsql)

    Thanks
    I asked what Openrsitem is all you did was repeated what was in your first post we can only guess what this Openrsitem does.

  7. #7
    Addicted Member
    Join Date
    Dec 11
    Posts
    246

    Re: Search with Like

    Hi Datamiser

    O.K this is hard coded 12 , what should be the sql like statement if value is to get from the variable.

    Thanks

  8. #8
    Fanatic Member
    Join Date
    Mar 09
    Posts
    721

    Re: Search with Like

    Are you using DAO or ADO ?

    In VB6 DAO and JET databases I always used the * as a wild card.

    SELECT * FROM m_item WHERE icode like '12*'

    should work just fine and will find strings that start with '12'

    If you want strings where '12' appears anywhere in the string then '*12*' should do it.

    I mentioned VB6 DAO and JET. if you were doing this with ADO or in .Net and oledb then you would need to use the '%' percent symbol. But for DAO you can use the * symbol.

  9. #9
    Addicted Member
    Join Date
    Dec 11
    Posts
    246

    Re: Search with Like

    Hi Ians

    12 is hardcoded , what should i need to do if i have to get value from a variable.

    Thanks

  10. #10
    Fanatic Member
    Join Date
    Mar 09
    Posts
    721

    Re: Search with Like

    Do you mean 12 is a variable?

    Dim someValue as String

    someValue = "12" '// Or any other string - perhaps from an input box or something

    esql = "SELECT * FROM m_item WHERE icode LIKE '" & someValue & "*' "

    It may be difficult to see the ' and " symbols above but after LIKE theres a ' and a " very close together.

  11. #11
    Addicted Member
    Join Date
    Dec 11
    Posts
    246

    Re: Search with Like

    Dear Ians


    I have written this but is is not searching whereas record exits

    strsearch = InputBox("Search For", "Search Input Box")
    strsql = "select * from m_itemmaster where icode LIKE '" & strsearch & "%' " . I am using ADO

    Thanks

  12. #12
    Fanatic Member
    Join Date
    Mar 09
    Posts
    721

    Re: Search with Like

    It's been many years since I did anything with VB6 and ADO. I always found DAO to be faster when accessing a JET/Access database. I only used ADO when accessing a SQL Server database.

    So I can't remember if VB6/ADO/Jet database used % or * as the wildcard.

    I assume you've tried using * symbol instead ?

    You're sure the data exists in your database as a string with no leading spaces ? - also make sure the data from the Inputbox does not have any leading/trailing spaces

    Can you show the whole code section ?

  13. #13
    Fanatic Member
    Join Date
    Mar 09
    Posts
    721

    Re: Search with Like

    It's been many years since I did anything with VB6 and ADO. I always found DAO to be faster when accessing a JET/Access database. I only used ADO when accessing a SQL Server database.

    So I can't remember if VB6/ADO/Jet database used % or * as the wildcard.

    I assume you've tried using * symbol instead ?

    You're sure the data exists in your database as a string with no leading spaces ? - also make sure the data from the Inputbox does not have any leading/trailing spaces

    Can you show the whole code section ? I mean you show us how you build the esql string but you don't show how you're using it or retrieving any values from the database so posting the whole section from collecting the user input all the way to evaluating the DB return values.

  14. #14
    Addicted Member
    Join Date
    Dec 11
    Posts
    246

    Re: Search with Like

    Hi Ians

    Problem is when i search with variable , if i harcode it it works fine.

    Dim rstitem as adodb.recordset

    strsql = "select * from m_itemmaster order by icode"
    Openrstitem (strsql)

    Private Sub Search()
    Dim strsearch, strsql As String
    strsearch = InputBox("Search For", "Search Input Box")
    strsql = "select * from m_itemmaster where icode LIKE '" & Trim(strsearch) & "%' "
    Openrstitem (strsql)
    if rstitem.recordcount > 0 then
    call display
    endif
    End Sub



    icode
    A00LB0C25343
    A00LO2C27338
    A00SW0C19975
    A01SC2C26652
    A09BT0C26400
    A09GT0C26400
    A09ST0C25589
    A12ST0C25236
    E12BT0C23896
    E12ST0C23896

  15. #15
    Fanatic Member
    Join Date
    Mar 09
    Posts
    721

    Re: Search with Like

    If you were searching for '12' and wanted to get results where '12' appears ANYWHERE in the string such as A12ST0C25236 and E12BT0C23896 then you need to put wildcard at both sides of the variable

    strsql = "select * from m_itemmaster where icode LIKE '%" & strsearch & "%' "

  16. #16
    Hyperactive Member
    Join Date
    Jun 12
    Location
    I'm living in VBForum bcz its members deserve respect and appreciation
    Posts
    318

    Re: Search with Like

    More like:
    Code:
    strsql = "SELECT * FROM [m_itemmaster] WHERE [icode] LIKE '%" & Trim(strsearch) & "%'"

  17. #17
    Addicted Member
    Join Date
    Dec 11
    Posts
    246

    Re: Search with Like

    Hi brss

    Thanks a lot , it is working .

    Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •