Results 1 to 13 of 13
  1. #1
    Sam is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Nov 2022
    Posts
    50

    Query Criteria

    I have a query where I have specified criteria as;


    Code:
    WHERE (((tblEnquiries.EnqName)=[Enter Cust Name]))
    I want to add the condition that if nothing is entered in the Input Box, All records to be displayed.

    How should I do that ?

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,933
    Code:
    SELECT Employees.[First Name]
    FROM Employees
    WHERE (((Employees.[First Name])=[Enter name])) OR (([Enter name] Is Null));
    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
    Sam is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Nov 2022
    Posts
    50
    That gives me the Input Box twice, and displays all records, because of the second condition

    ETA - That works, thanks. The error was because I just copied your tbl and field names.

  4. #4
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,933
    Quote Originally Posted by Sam View Post
    That gives me the Input Box twice, and displays all records, because of the second condition

    ETA - That works, thanks. The error was because I just copied your tbl and field names.
    Never a good idea.

    That is because I tested that query before posting, as I was not sure that was the syntax.
    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
    Sam is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Nov 2022
    Posts
    50
    Quote Originally Posted by Welshgasman View Post
    Never a good idea.

    That is because I tested that query before posting, as I was not sure that was the syntax.
    There is no icon for a knock on the head, but consider it done.

  6. #6
    Sam is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Nov 2022
    Posts
    50
    Further Question
    If we want to avoid user error in typing the Names, can this be done with a drop down list, like a filter ?

  7. #7
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    You can use a combo box on a form with the names / ID's as the rowsource and use the same syntax in the query:

    Code:
    WHERE  tblEnquiries.EnqName = Forms![YourFormName].[cmbYourCombo] or Forms![YourFormName].[cmbYourCombo] is Null
    As per last time replace with your form and control names.
    In the query designer it should bring up the names for you once you type forms! in the criteria box.
    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 ↓↓

  8. #8
    Sam is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Nov 2022
    Posts
    50
    I'm doing that but draw a blank each time.
    Code:
    WHERE tblEnquiries.EnqName = Forms![frmDropDown].[cbodropdown] or Forms![frmDropDown].[cbodropdown] is Null
    Is it because after criteria selection I'm using a Run Query button. The button has this On Click
    Code:
    DoCmd.OpenQuery "qryBilling"
     Me.cbodropdown = ""

  9. #9
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    Can you post up a zipped copy of your database?
    Only need some sample data and the forms you are having an issue with.
    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 ↓↓

  10. #10
    Sam is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Nov 2022
    Posts
    50
    Sure. Here we go. But I have removed the DropDown form from this. Maybe you can suggest some other optimizations as well.
    V 7.0 Trials.zip

  11. #11
    Sam is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Nov 2022
    Posts
    50
    This one has everything. But a WiP.

    V 7.0 TrialsM.zip

  12. #12
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    Which form should I be looking at?

    Edit: ignore me I worked it out

    Your problem is cause by referring to the visual combo results not the bound column.
    Your combo is storing the EnqID not their name so you need to use the criteria against that:

    Code:
    SELECT tblBilling.BillID, tblEnquiries.EnqName, qryBookingValues.WIng, qryBookingValues.UnitNo, tblBilling.BillNo, tblBilling.BillDate, tblBilling.BillPercent, tblBilling.BillDetails, qryBookingValues.ValAgr, qryBookingValues.ValGSTAgr, [ValAgr]*[BillPercent]/100 AS ValBillAgr, [ValGSTAgr]*[BillPercent]/100 AS ValBillGST, [ValBillAgr]+[ValBillGST] AS ValBillTotal
    FROM tblEnquiries INNER JOIN ((tblBilling INNER JOIN tblBookings ON tblBilling.BookingID = tblBookings.BookingID) INNER JOIN qryBookingValues ON tblBookings.BookingID = qryBookingValues.BookingID) ON tblEnquiries.EnqID = tblBookings.EnqID
    WHERE (((tblBookings.EnqID)=[Forms]![frmDropDown].[cbodropdown] Or [Forms]![frmDropDown].[cbodropdown] Is Null))
    ORDER BY tblBilling.BillID;
    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
    Sam is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Nov 2022
    Posts
    50
    Thanks Minty,
    I have it working now.

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

Similar Threads

  1. Replies: 2
    Last Post: 09-25-2020, 10:39 AM
  2. Replies: 2
    Last Post: 04-02-2015, 12:45 PM
  3. Replies: 4
    Last Post: 08-30-2014, 10:23 AM
  4. Replies: 5
    Last Post: 08-02-2012, 09:44 AM
  5. Replies: 1
    Last Post: 07-13-2011, 11:00 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