Results 1 to 26 of 26

Thread: LIKE Operator with a Text Box Input

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2004
    Posts
    10

    LIKE Operator with a Text Box Input

    Sir,
    I have tried every possible syntax bit no success. I need to input a value in a search text box named searchtext.text using my Access 2000 search button and the search should include the LIKE operator e.g In the Search box I type "Medicine" and all records like Nigerian Medicine Accociation, Japan Medicine Group etc should come out of my database table.

    Pleas give a simple code in SQL/VB.
    Thanks in advance

    Azam
    Azam

  2. #2
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263
    The SQL query would be:

    Code:
    SELECT * FROM SOMETABLE_T WHERE SOMECATEGORYFIELD LIKE '*MEDICINE*'
    (btw - I use MS SQL - so we use % as the wildcard character - I believe I've seen posted around here that ACCESS uses * for the wildcard character).

    Do you need help build the SQL query string in VB itself??

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2004
    Posts
    10

    LKE with a Text Box

    Dear Sir,
    Thanks a lot for the quick response.I admire.But I wanted code for LIKE operator with search input coming form a TEXT Box while you provided simple input i.e *MEDICINE* please if possible modify and send it again as soon as possible as my project is stuck.
    Thanks
    Azam

  4. #4
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263
    The way things work best around here - so we can expedite our answers in the least amount of re-posting, is for you to post what you have right now.

    It would be nice to see your connection string - your attempt so far to make a query come back into a recordset. Even the name of your textbox would be helpful.

    Put the word VBCODE in [] brackets at the top of the code you post - and end it with /VBCODE in [] brackets. That makes it easier to read code posted on the forum.

  5. #5
    Hyperactive Member Granty's Avatar
    Join Date
    Mar 2001
    Location
    London
    Posts
    439
    Code:
    SELECT * FROM SOMETABLE_T WHERE SOMECATEGORYFIELD LIKE '*" & MyTextBox.Value & "*' "

  6. #6
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263
    Originally posted by Granty
    Code:
    SELECT * FROM SOMETABLE_T WHERE SOMECATEGORYFIELD LIKE '*" & MyTextBox.Value & "*' "
    My textbox objects have a .TEXT property - not a .VALUE property

  7. #7
    Hyperactive Member Granty's Avatar
    Join Date
    Mar 2001
    Location
    London
    Posts
    439
    I thought this was access....

    AFAIK in access you cant reference the .Text property unless the control has focus...

  8. #8

    Thread Starter
    New Member
    Join Date
    Oct 2004
    Posts
    10

    LIKE with Textbox Input

    Dear Sir,

    So you seem to be online right now.The code is here:

    VB Code:
    1. Dim strSQL As String
    2.  
    3.  strSQL = "SELECT  * FROM JournalsTable WHERE Title = '" & Trim(txtSearch.txt) & "'"
    4.    
    5.     Me!txtSearch.RowSource = strSQL
    6.     Me!txtSearch.Requery
    7.    
    8.    
    9.     DoCmd.ShowAllRecords
    10.     DoCmd.GoToControl ("ComboTitleData")
    11.     DoCmd.FindRecord Me!txtSearch
    12.  
    13.      
    14.     txtSearch.SetFocus
    15.     strStudentRef = Title
    16.     txtSearch.SetFocus
    17.     strSearch = txtSearch.Text

    I am working in Access2000
    Azam

  9. #9
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263
    That's what I hate about these vague threads - ACCESS 2000 or SQL/VB - both are mentioned in the first post...

    No code samples - no idea what this person knows or doesn't know...

    I'm leaving this thread - good luck

  10. #10

    Thread Starter
    New Member
    Join Date
    Oct 2004
    Posts
    10
    I wonder why my thread annoyed Mr. Granty.I mentioned I was using VB/SQL and both of these are used in Access2000 in which I am making a database.Anyway I am sorry for the disturbance I created.I am trying Mr.Granty's code righht now with a Value property.Hope it works.
    Azam

  11. #11
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343
    Azam you are going to want to re-think your route to solve this .. or at least re-read some tutorials/examples.. Thats not how you search and prolly never will be!

    You have docmd, so that means you are In Access.

    Are you using Bound controls on a form?
    How much do you know about Access?
    Are you trying to put in a search form, which has a listbox or other form as the output?
    What are you trying to do?


    (edit) Technically its VBA in Access and not VB... which is similar but different (/edit)


    Vince

    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...

  12. #12
    Hyperactive Member Granty's Avatar
    Join Date
    Mar 2001
    Location
    London
    Posts
    439
    Me?

    Im not annoyed, I was just trying to help.....

  13. #13
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263
    I can't say that I'm really annoyed - but this is a VB forum and this is the DATABASE spot in it - where we talk about VB and SQL (and other backend databases).

    And I knew after the third post that this was going south fast - I love to help - but I don't know ACCESS and bound-data-controls are evil (at least in VB - my opinion).

    Good luck

  14. #14
    Hyperactive Member Granty's Avatar
    Join Date
    Mar 2001
    Location
    London
    Posts
    439
    Its all very well you getting the hump, but its ME who's getting the grump reputation here

  15. #15

    Thread Starter
    New Member
    Join Date
    Oct 2004
    Posts
    10
    Sorry Sorry I misread, it was Mr szlamany who got annoyed .

    By now I have messed up with my code still I am attaching my file for looking at the form atleast.I need to put text in the search box ,click "Confirm" button and the record should fill in the fields below.But again I need the LIKE operator for searching.

    I am actually modifying a sample code and I did it well before going for this LIKE exercise and I can do it again once the LIKE problem is solved.
    Attached Files Attached Files
    Azam

  16. #16

    Thread Starter
    New Member
    Join Date
    Oct 2004
    Posts
    10
    Please only refer to the Journalsform in the attached file db3.zip
    Azam

  17. #17
    Hyperactive Member Granty's Avatar
    Join Date
    Mar 2001
    Location
    London
    Posts
    439
    I'll try and take a look a bit later, Im actually supposed to be working atm.

  18. #18

    Thread Starter
    New Member
    Join Date
    Oct 2004
    Posts
    10
    I am really grateful.I'll be waiting ,and will come again tommorrow.
    Thanks
    Azam

  19. #19
    Hyperactive Member Granty's Avatar
    Join Date
    Mar 2001
    Location
    London
    Posts
    439
    Azam,

    I never work with bound controls so Im not really sure what Im doing or what most of the docmd stuff in your form does.

    However, I have got it to work sort of, but all the specifics you need you will have to play with them yourself Im afraid.

    This is the code I put into JournalsForm

    Code:
    Option Compare Database
    
    
    '--------------------------------------------------------------
    'Graham Thorpe 25-01-02
    '--------------------------------------------------------------
    
    '---------------------------------------------------------------
    
    Private Sub FindAndPopulateTitle()
    
    
          Dim strSQL As String
    'Performs the search using value entered into txtSearch
    'and evaluates this against values in strStudentID
        strSQL = "SELECT  * FROM JournalsTable WHERE Title LIKE '*" & Trim(txtSearch.Value) & "*' "
        
        Me.RecordSource = strSQL
        Me.Requery
        
        
        'txtSearch = Me!strSQL
        DoCmd.ShowAllRecords
        DoCmd.GoToControl ("ComboTitleData")
        DoCmd.FindRecord Me!txtSearch
        'Me!txtSearch
         
        txtSearch.SetFocus
        strStudentRef = Title
        txtSearch.SetFocus
        strSearch = txtSearch.Text
            
    End Sub
    
    'If matching record found sets focus in strStudentID and shows msgbox
    'and clears search control
    
    Private Sub cmdButtonFind_Click()
        Dim strStudentRef As String
        Dim strSearch As String
      
    'Check txtSearch for Null value or Nill Entry first.
    
        If IsNull(Me![txtSearch]) Or (Me![txtSearch]) = "" Then
            MsgBox "Please enter a value!", vbOKOnly, "Invalid Search Criterion!"
            Me![txtSearch].SetFocus
        Exit Sub
    End If
       
       FindAndPopulateTitle
       
       
        'If strStudentRef = strSQL Then
        'strSearch Then
         '   MsgBox "Match Found For: " & strSearch, , "Congratulations!"
         '   ComboTitleData.SetFocus
         '   txtSearch = ""
            
        'If value not found sets focus back to txtSearch and shows msgbox
          '  Else
          '      MsgBox "Match Not Found For: " & strSearch & " - Please Try Again.", _
          '      , "Invalid Search Criterion!"
          '      txtSearch.SetFocus
        'End If
    End Sub
    Private Sub cmdButtonFind_AfterUpdate()
    'Once a record is selected in the list, enable the showRecord button
       CmdButtonFind.Enabled = True
    CmdButtonFind.SetFocus
    End Sub
    How it helps somewhat....

  20. #20

    Thread Starter
    New Member
    Join Date
    Oct 2004
    Posts
    10
    Dear Granty,
    I am really impressed by your quick and considerate detailed responses.I am waiting for the weekend to try the modification you sent and will let you know it it works.
    Azam

  21. #21
    New Member
    Join Date
    Apr 2018
    Posts
    3

    Re: LIKE Operator with a Text Box Input

    [QUOTE=szlamany;1820837]The SQL query would be:

    Code:
    SELECT * FROM SOMETABLE_T WHERE SOMECATEGORYFIELD LIKE '*MEDICINE*'
    I also have the same problem but in visual studio.. what syntax needs to be used?? i'm using MySql 5.6.39

  22. #22
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: LIKE Operator with a Text Box Input

    Quote Originally Posted by Dhanya View Post
    I also have the same problem but in visual studio.. what syntax needs to be used?? i'm using MySql 5.6.39
    The SQL code is still executed by the database, so it doesn't matter where you write the code.

  23. #23
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: LIKE Operator with a Text Box Input

    Start a new thread - do not post in a 14 year old thread!

    geez!

    Welcome to the forum!!!

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  24. #24
    New Member
    Join Date
    Apr 2018
    Posts
    3

    Re: LIKE Operator with a Text Box Input

    But it is showing error in syntax..
    my code goes like this
    select service_id, service_name from tblService where service_desc like '" + servicename.Text +"' "
    what is wrong in this syntax?

  25. #25
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,440

    Re: LIKE Operator with a Text Box Input

    ......... LIKE '%"+servicename.text+"%'"
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  26. #26
    New Member
    Join Date
    Apr 2018
    Posts
    3

    Re: LIKE Operator with a Text Box Input

    Thank you so much.... It worked

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