Results 1 to 7 of 7
  1. #1
    keith2511 is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2017
    Location
    UK
    Posts
    45

    Searching with multiple criteria

    I built a form for colleagues to use, to search a single field in one table for a single word. The SQL is



    Code:
    HAVING (((All_Invoices.Product_Description) Like "*" & [Forms].[Invoice_SearchForm].[EnteredText] & "*"))
    My colleagues now want to search for up to five words at the same time and my initial thought was to add another four text boxes to the form. This works if all five text boxes contain text but if any are left blank the query returns all of the data in the table field.

    I have had a suggestion that amending my code to insert an additional leading and trailing quotation marks i.e.

    Code:
    HAVING (((All_Invoices.Product_Description) Like ""*" & [Forms].[Invoice_SearchForm].[EnteredText] & "*""))
    and getting the user to add multiple criteria to the text box, delimited by an asterix would work i.e. apple*orange*pear

    but this produces a Data type mismatch in criteria expression error.

    Grateful for any suggestions on how I can achieve my goal.

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Strings need to be surrounded by single quotes ' unless it contains a single quote, then triple double quotes works, I think?


    Date literals with # and in mm/dd/yyyy or yyyy-mm-dd format


    Numbers do not need anything


    Also for anything other than one item of criteria, I tend to put the the criteria into a string variable and then debug.print it, until correct then also use that in the code.


    Added benefit is, if you still cannot see the error, you can copy and paste back here for someone else to spot it.
    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
    xps35's Avatar
    xps35 is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Jun 2022
    Location
    Schiedam, NL
    Posts
    232
    You do need 5 textboxes. The idea is to build a condition string depending on which are not blank. In short:
    Code:
    Dim condition as string
    
    condition = "like *" & txt1 & "*"
     
    If not IsNull txt2 then
       condition =condition & " Or like *" & txt2 & "*" 
    EndIf
    
    If not IsNull txt3 then
       condition =condition & " Or like *" & txt3 & "*"
    EndIf
    
    . 
    . 
    .
    Groeten,

    Peter

  4. #4
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    I would also be using a WHERE clause, not HAVING?

    https://www.geeksforgeeks.org/differ...clause-in-sql/
    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

  5. #5
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Office 365
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,977
    Quote Originally Posted by Welshgasman View Post
    I would also be using a WHERE clause, not HAVING?

    https://www.geeksforgeeks.org/differ...clause-in-sql/
    Agreed. See also Speed Comparison Tests 4 (isladogs.co.uk)
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  6. #6
    journeyman is offline Advanced Beginner
    Windows 11 Access 2016
    Join Date
    Dec 2022
    Posts
    82
    <<Removed and replaced>>

  7. #7
    journeyman is offline Advanced Beginner
    Windows 11 Access 2016
    Join Date
    Dec 2022
    Posts
    82
    You do need 5 textboxes.
    You can also possibly use Split function so that you could search on all words separated by a space within 1 text box. Save yourself from making 5 text boxes cluttering up the place.

    Something like... (Air code - not tested)

    Code:
    Dim mySplitVariant as variant
    Dim condition as string
    Dim i as integer
    
         MySplitVariant = Split(me.txtSearch, " ")     
         condition = "like *" & MySplitVariant(0) & "*"
         For i = 1 to ubound(mysplitVariant)
              condition =condition & " Or like *" & MySplitVariant(i) & "*"
         Next i

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

Similar Threads

  1. Replies: 1
    Last Post: 12-22-2015, 02:52 AM
  2. Replies: 6
    Last Post: 07-27-2015, 10:23 AM
  3. Replies: 1
    Last Post: 02-18-2011, 01:40 AM
  4. Replies: 1
    Last Post: 12-22-2010, 01:28 AM
  5. Replies: 2
    Last Post: 05-25-2010, 02:45 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