Results 1 to 9 of 9
  1. #1
    journeyman is offline Advanced Beginner
    Windows 11 Access 2016
    Join Date
    Dec 2022
    Posts
    82

    Display found record OR display all records

    Hi all,



    I am building a query to display a found record based on a string criteria - easy mode!

    Except - If that query does not return a result, then I would like it to return all results (either table or by criteria)... including results where the criteria field is null.

    So far I'm trying:

    Code:
    SELECT [CriteriaField], IIF([CriteriaField] LIKE '*ABC*', [CriteriaField], IIF([CriteriaField] Is Null, 'Empty Record', [CriteriaField]))
    FROM MY_Table
    However, this query returns all records in the table. I tried using the criteria field also...:

    Code:
    SELECT CriteriaField 
    FROM My_Table
    WHERE IIF([CriteriaField] LIKE '*ABC*', [CriteriaField], IIF([CriteriaField] Is Null, 'Empty Record', [CriteriaField]))

    So.... neither works except to output the entire table....

    I need either the result, or everything where the result failed - all tidy in one crazy query.

    Any thoughts?

    Cheers

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Is this what you're after?

    http://www.theaccessweb.com/queries/qry0001.htm

    If you actually want to change the behavior when the query returns no records with a criteria, you'll probably have to test it in code are react accordingly.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    journeyman is offline Advanced Beginner
    Windows 11 Access 2016
    Join Date
    Dec 2022
    Posts
    82
    Thanks for your quick response.

    Unfortunately, no. The following will cater to and display the NULL field.

    Code:
    IIF([CriteriaField] Is Null, 'Empty Record', [CriteriaField])
    I am looking to either find a criteria result(s) .... OR return every record including any records that are null.

    Thanks again

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Not on a computer, but I think you'll have to test the SQL somehow, then return filtered or all records as appropriate. Knowing more specifics or seeing your current code would help.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    I'm not quite grasping your expected results.
    Which of these describes your desired outcome?


    1. If no records match the criteria return all of the records.
    2. If no records match the criteria return return only the records where the criteria field is null.
    3. Return the records that match the criteria AND where the criteria field is Null
    4. Return just the records that match your criteria


    Some other combination I can't think of?
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  6. #6
    DenShin is offline Novice
    Windows 10 Office 365
    Join Date
    Aug 2023
    Posts
    8
    Click image for larger version. 

Name:	Need.PNG 
Views:	18 
Size:	22.5 KB 
ID:	50702
    I'm kinda having the same problem, however I'm using the expression builder, is there a way to get what I need?

    1. Return the exact records that match the criteria, and returns all the record if the text box is empty or null.

    For now, I'm using the Like "*" & txt.value & "*" upon my text box is empty, this display all the records on the database and returns only the records based from the value inside the text box. However, this has a negative side to it.

    Example: I want to find all the records of my staffs. The records of RONALD and RONA.
    Using the expression above, If RONALD is the value of the text box, this returns all records associated to RONALD. However, when I look for RONA's works the expression returns both RONALD and RONA's works.

    Is there a way for needs to get?

    Thank you in advance for the help.
    Last edited by DenShin; 09-01-2023 at 12:21 AM. Reason: adding photo

  7. #7
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    See the link in post#2

  8. #8
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Quote Originally Posted by DenShin View Post
    Click image for larger version. 

Name:	Need.PNG 
Views:	18 
Size:	22.5 KB 
ID:	50702
    I'm kinda having the same problem, however I'm using the expression builder, is there a way to get what I need?

    1. Return the exact records that match the criteria, and returns all the record if the text box is empty or null.

    For now, I'm using the Like "*" & txt.value & "*" upon my text box is empty, this display all the records on the database and returns only the records based from the value inside the text box. However, this has a negative side to it.

    Example: I want to find all the records of my staffs. The records of RONALD and RONA.
    Using the expression above, If RONALD is the value of the text box, this returns all records associated to RONALD. However, when I look for RONA's works the expression returns both RONALD and RONA's works.

    Is there a way for needs to get?

    Thank you in advance for the help.
    Do not use the leading * ?
    However if they are matching for the same characters, that is always going to happen.
    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

  9. #9
    DenShin is offline Novice
    Windows 10 Office 365
    Join Date
    Aug 2023
    Posts
    8
    Quote Originally Posted by Welshgasman View Post
    Do not use the leading * ?
    However if they are matching for the same characters, that is always going to happen.

    Thank you for the response @Welshgasman, I've tried it and as you said, always going to happen. I also used the "IIF" still the same applies on it. Do you think I should make it on VBA code instead of the Expression Builder to get what I want for the result? If so, can someone help me how to make the code for it.
    I have a "TableDatabase" and make "QueryData" out of the TableDatabase and I'm using the QueryData to display the records with the CRITERIA just like the above. I've been trying to make the code, but still I can't get it. The textbox im using is unbound one, this is the textbox where I input the CRITERIA to search on the records.

    Tbl = Table
    QDat = QueryData
    Noted = Textbox

    Again, thank you in Advance.

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

Similar Threads

  1. Replies: 1
    Last Post: 04-11-2022, 02:52 PM
  2. Replies: 5
    Last Post: 05-25-2021, 09:55 AM
  3. Replies: 2
    Last Post: 01-26-2021, 06:44 PM
  4. Replies: 3
    Last Post: 01-18-2020, 07:54 PM
  5. Display all the lookup values found
    By boboivan in forum Access
    Replies: 2
    Last Post: 02-22-2016, 07:40 AM

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