Results 1 to 13 of 13
  1. #1
    Mikea8000 is offline Novice
    Windows 10 Access 2019
    Join Date
    Sep 2023
    Posts
    6

    Red face Parameter Query help needed

    I am passing several (2 to start) values from a form combo boxes.



    I want to select where company name = Amazon, "and/or" where the value is $100 dollars. If one of the two fields is null for some reason the querty is not quite working. Can someone PLEASE help me re-write this query so if functions in all cases with multiple combo boxes I am a bit off.
    I don't want to to be always an "AND" or always a "OR" just want the query to return the values in the populated combo boxes and if null skip the combo box.


    SELECT MasterImport.Cost, MasterImport.Company_Name, *
    FROM MasterImport
    WHERE (((MasterImport.Cost)=IIf([Forms]![frmQuery]![cboCost] Is Not Null,[Forms]![frmQuery]![cboCost],Null)) AND ((MasterImport.Company_Name)=IIf([Forms]![frmQuery]![cboCompanyname] Is Not Null,[Forms]![frmQuery]![cboCompanyname],Null)));

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    I don't use dynamic parameters in query object. I prefer VBA to build filter and apply to form or report. Review http://allenbrowne.com/ser-62.html

    Cannot compare anything to NULL because there is nothing to compare. So = Null is bound to fail in any circumstance.

    Try:
    Code:
    WHERE (((MasterImport.Cost) LIKE IIf([Forms]![frmQuery]![cboCost] Is Not Null, Val([Forms]![frmQuery]![cboCost]), "*")) 
    AND ((MasterImport.Company_Name) LIKE IIf([Forms]![frmQuery]![cboCompanyname] Is Not Null, [Forms]![frmQuery]![cboCompanyname], "*")));
    
    Or explore the query example at end of Allen Browne's article.
    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.

  3. #3
    Mikea8000 is offline Novice
    Windows 10 Access 2019
    Join Date
    Sep 2023
    Posts
    6
    I figured it out finally:

    SELECT MasterImport.Cost, MasterImport.Company_Name, *
    FROM MasterImport
    WHERE (((MasterImport.Cost) Like "*" & [Forms]![frmQuery]![cboCost] & "*") AND ((MasterImport.Company_Name) Like "*" & Trim([Forms]![frmQuery]![cboCompanyname]) & "*"));

  4. #4
    Mikea8000 is offline Novice
    Windows 10 Access 2019
    Join Date
    Sep 2023
    Posts
    6
    Thank you for replying, much appreciated! This worked well! I see a lot other posters trying to do the same thing but do not know how to ask the question in a way that gets them the working results for NULL values. I have to save this for sure.

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Are you sure that gets you what you want? LIKE *10* will return everything with 10 in it: 100, 1000, 40.10, etc. LIKE 10 returns only 10, same as = sign.

    Same would happen with company name if there are names that are similar enough. LIKE *Carson, Inc* would return Carson, Inc as well as Kit Carson, Inc.
    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.

  6. #6
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    The best route is to simply add the following Is Null to each combo reference:
    Code:
    ELECT MasterImport.Cost, MasterImport.Company_Name, *
    FROM MasterImport
    WHERE MasterImport.Cost  = ([Forms]![frmQuery]![cboCost]  OR [Forms]![frmQuery]![cboCost]  is Null)
    AND MasterImport.Company_Name  = ( [Forms]![frmQuery]![cboCompanyname] OR [Forms]![frmQuery]![cboCompanyname] is Null)
    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 ↓↓

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    As shown in Allen Browne example.

    Minty, think your opening parens are misplaced.
    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.

  8. #8
    Mikea8000 is offline Novice
    Windows 10 Access 2019
    Join Date
    Sep 2023
    Posts
    6
    Well I just realized it's close but it's returning not returning records where there are NULL values.

    So I am writing update statements for all nulls since the table is pretty small. However, I would like to have it return nulls as part of my expression builder.
    Suggestions. Also you right it returns extra stuff for the wildcards which matters (not exact) how to fix both?

  9. #9
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Minty's (also Allen Browne's) approach should work. Did you try?
    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.

  10. #10
    Mikea8000 is offline Novice
    Windows 10 Access 2019
    Join Date
    Sep 2023
    Posts
    6
    Yes I tried Minty's but it's not returning the records that have NULL(or blank) in any part of the query/fields. It looks like it should work in theory but is skipping the NULL records for some reason.

  11. #11
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    The NULL records return only if there is no input in the control. Works for me. If you expect something else, please clarify.

    I think Minty's shows misplaced opening parentheses.

    Post your attempted SQL
    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.

  12. #12
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    I agree - the query won't return nulls if you have some criteria.
    If you want it to you would need to add OR Is Null to the field criteria, not the combobox expression.

    Post up the sql you have now and/or a sample database, also some demonstration of your expected results, e.g. Starting data, your criteria, and what you want as a output, you can mock it up in excel if necessary.
    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 ↓↓

  13. #13
    Join Date
    May 2018
    Location
    Living in Scotland UK
    Posts
    1,566
    an you post a screenshot of the query in design view?
    You can PM me if you need further help.
    Good Reading https://docs.microsoft.com/en-gb/off...on-description

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

Similar Threads

  1. Replies: 9
    Last Post: 04-15-2015, 12:05 PM
  2. Replies: 2
    Last Post: 10-16-2014, 09:46 AM
  3. Parameter Query Help Needed
    By Lynnemcc87 in forum Access
    Replies: 9
    Last Post: 08-13-2014, 08:37 AM
  4. Replies: 12
    Last Post: 06-25-2013, 12:52 PM
  5. HELP NEEDED! Enter Parameter Value
    By lpfluger in forum Queries
    Replies: 4
    Last Post: 03-27-2011, 04:38 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