Results 1 to 7 of 7
  1. #1
    Khalil Handal is offline Competent Performer
    Windows 10 Access 2010 32bit
    Join Date
    Jun 2016
    Posts
    237

    multiple criteria for one field

    Hi ,


    I have this query and I obtain results from it.
    Code:
    SELECT FieldName FROM TableName WHERE FieldName Like "*Word 1*";
    I am trying to add another criteria to the same field. FieldName Like "*Word 1*" AND Like "*Word 2*" with out success.

    In other words I want to search for records with both criteria. The table represents the many side of a one-to-many relationship.
    Any ideas ?

    Khalil

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,914
    So that would be OR not AND ?
    You say both criteria, but I am assuming either of both criteria.? How can the fieldname be Word1 AND Word2? One negates the other?
    Or just search for Word?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    Join Date
    Apr 2017
    Posts
    1,679
    Code:
    SELECT FieldName From TableName WHERE FieldName IN (Name1; Name2; ...; NameN)
    , but I'm almost sure you can't use LIKE there.

  4. #4
    Khalil Handal is offline Competent Performer
    Windows 10 Access 2010 32bit
    Join Date
    Jun 2016
    Posts
    237
    Thanks
    Is there a way to use the Like?

  5. #5
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,114
    Yes, using the OR operator instead of AND as explained by Welshgasman. You could also look at the Instr() function:
    Code:
    Instr([YourField],"Word")>0
    Adding the above calculated field to your query with True in the criteria row would return all records containing "Word" in that field.
    https://support.microsoft.com/en-us/...8-77cd0cb8a55b
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    So given a data sample like:
    ID Key_FK Word
    1 1 Word1
    2 1 Word2
    3 1 Word3
    4 2 Word1
    5 2 Word2
    6 3 Word3
    7 3 Word4
    8 4 Word1
    9 5 Word2

    You would want to retrieve only Key_FK 1 and 2 because those Key_FK are associated with both Word1 and Word2?
    Selecting records based on data in another record of same table is never simple.

    Do you want to return the entire record or just the Key_FK? Would you want to return record ID 3 along with ID 1 and ID 2?

    Consider:
    SELECT Words.* FROM Words
    WHERE Key_FK In (SELECT Key_FK FROM Words WHERE Word LIKE "*Word1*") AND Key_FK IN (SELECT Key_FK FROM Words WHERE Word LIKE "*Word2*");

    also:
    SELECT Words.* FROM Words
    WHERE (((Words.Word) Like "*Word1*" Or (Words.Word) Like "*Word2*") AND ((Words.[Key_FK]) In (SELECT Key_FK FROM Words WHERE Word LIKE "*Word1*") And (Words.[Key_FK]) In (SELECT Key_FK FROM Words WHERE Word LIKE "*Word2*")));



    Now, assuming the Word value will not be repeated for each Key_FK, consider:
    SELECT Words.Key_FK, Count(IIf([Word] Like "*Word1*" Or [Word] Like "*Word2*",1,Null)) AS Cnt
    FROM Words
    GROUP BY Words.Key_FK
    HAVING (((Count(IIf([Word] Like "*Word1*" Or [Word] Like "*Word2*",1,Null)))=2));

    Anything more complicated than 2 words should probably be handled with a custom VBA function.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    Khalil Handal is offline Competent Performer
    Windows 10 Access 2010 32bit
    Join Date
    Jun 2016
    Posts
    237
    Thank you for he reply. I will try it and keep you posted.
    Khalil

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 2
    Last Post: 01-28-2019, 06:32 AM
  2. Query for multiple criteria for 1 field
    By bednarol in forum Queries
    Replies: 9
    Last Post: 09-21-2018, 07:07 PM
  3. Replies: 2
    Last Post: 05-17-2017, 06:17 AM
  4. Multiple criteria in same field
    By Access_Novice in forum Queries
    Replies: 3
    Last Post: 05-03-2015, 09:13 AM
  5. consultation with field for multiple criteria
    By fabiobarreto10 in forum Queries
    Replies: 21
    Last Post: 03-05-2012, 01:59 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums