Results 1 to 14 of 14
  1. #1
    zashaikh is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2017
    Posts
    182

    Use Form Fields to Run a Query


    Hello

    My coworkers are not that great with Queries. So I was hoping to make a Form that would include all my query fields, and then a button that would open said Query.

    For example, usually, I would open a query, select my tables and fields, and input the data I want from my query, such as "country, date range, products."

    Is there a way where I can make a Form where a user can input "country" into a field. And "date range" into a field. etc etc. Then they would click on a button and have the query open up?

    Thanks!

  2. #2
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,423
    Is there a way where I can make a Form where a user can input "country" into a field. And "date range" into a field. etc etc. Then they would click on a button and have the query open up?
    yes - for country, on a form called say frmSearch you would have an unbound textbox called say txtCountry

    in your query, to query a table called say myTable with a field called say Country you would have code along these lines

    SELECT *
    FROM myTable
    WHERE Country=forms!frmSearch!txtCountry

    for between dates you would have two unbound textboxes called say txtStart and txtEnd - and in your myTable table a field called say OpenDate. The query would look something like

    SELECT *
    FROM myTable
    WHERE OpenDate between "#" & forms!frmSearch!txtStart & "#" AND "#" forms!frmSearch!txtEnd & "#"

    and to do both

    SELECT *
    FROM myTable
    WHERE Country=forms!frmSearch!txtCountry AND OpenDate between "#" & forms!frmSearch!txtStart & "#" AND "#" forms!frmSearch!txtEnd & "#"

    You can see how the criteria changes depending on the requirement. Building a search form can become quite complex with multiple criteria, particularly if users do not complete all the txtboxes. So suggest google 'Access search forms' or similar for examples where this question has been asked many times before.

  3. #3
    zashaikh is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2017
    Posts
    182
    Thank y+ Reply to Threadou!. It was a lot easier than I though. But how do I enter in the Date range in the Query Design view? As opposed to SQL?

    Thanks

  4. #4
    zashaikh is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2017
    Posts
    182
    Also, in my search form, if I leave a field blank, such as city, I do not get any results. I think this has to do with my forms!frmSearch!txtCity.

    How do I make the query ignore the fact that I may leave fields blank intentionally on my search form?

  5. #5
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,423
    for example

    WHERE Country=forms!frmSearch!txtCountry OR forms!frmSearch!txtCountry is null

  6. #6
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Here is an example of a search form by Allen Browne.
    It actually does uses filtering, instead of a true search (limiting the records returned).
    http://allenbrowne.com/ser-62.html

  7. #7
    zashaikh is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2017
    Posts
    182
    Quote Originally Posted by Ajax View Post
    for example

    WHERE Country=forms!frmSearch!txtCountry OR forms!frmSearch!txtCountry is null

    I see... How do I enter a "WHERE" condition into my query? I have tried """"forms!frmSearch!txtCountry OR forms!frmSearch!txtCountry is null """"", but this finds ALL fields in my table that are blank and displays them on the query.

  8. #8
    zashaikh is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2017
    Posts
    182
    Quote Originally Posted by ssanfu View Post
    Here is an example of a search form by Allen Browne.
    It actually does uses filtering, instead of a true search (limiting the records returned).
    http://allenbrowne.com/ser-62.html
    Thank you. But I am specifically interested in having result in a Query as opposed to a Form

  9. #9
    Micron is offline Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,799
    Neither the example you quoted or your example look correct to me. With yours, you cannot omit the table/field reference that you want to compare to the form control value or Null. With the quoted one, it doesn't seem to me that the country field is being compare to Null as it was omitted.
    As long as we're talking query sql and not VBA, I think:
    Code:
    WHERE tblSomeTable.SomeField = forms!frmSearch.txtBox OR tblSomeTable.SomeField Is Null
    Last edited by Micron; 02-13-2017 at 02:17 PM. Reason: grammar
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  10. #10
    zashaikh is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2017
    Posts
    182
    Quote Originally Posted by Micron View Post
    Neither the example you quoted or your example look correct to me. With yours, you cannot omit the table/field reference that you want to compare to the form control value or Null. With the quoted one, it doesn't seem to me that the country field is being compare to Null as it was omitted.
    As long as we're talking query sql and not VBA, I think:
    Code:
    WHERE tblSomeTable.SomeField = forms!frmSearch.txtBox OR tblSomeTable.SomeField Is Null

    Thank you. But where do I enter this code? Do I just enter it in the criteria field in the Query Design view?

    Click image for larger version. 

Name:	temp.png 
Views:	14 
Size:	11.2 KB 
ID:	27488

  11. #11
    Micron is offline Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,799
    That would be the easiest way. Every line/row after the first one in that design view grid becomes an OR condition.

  12. #12
    zashaikh is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2017
    Posts
    182
    I think I am having trouble explaining myself. Let me start from the beginning. I want to explain what I WANT to be able to do.

    1. The image below is my Form.
    Click image for larger version. 

Name:	1.png 
Views:	13 
Size:	12.5 KB 
ID:	27503


    2. On my query, I have set up the following Criterias

    Click image for larger version. 

Name:	2.png 
Views:	13 
Size:	5.9 KB 
ID:	27504

    This works correctly.

    3. However, I want to be able to search for "Country 2" in my Form, I add [forms]![EZ_Query_Search]![txtCountry2] to my Query. When I do this, I MUST type in a country in the txtCountry2 field on my form. If I do not, the Query will not return any results. I want to BE ABLE TO type in the "Date Start, Date End, Country 1, Country 2, and the other fields" in my Form to conduct a query. However, I do not want to NEED to do that. If I do not care what "Country 2" is, I want the Query to show all results where Country1 is Iran.

    I hope this makes sense...

  13. #13
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,423
    in your identified country column change the criteria from

    [forms]![EZ_Query_Search]![txtCountry]

    to

    [forms]![EZ_Query_Search]![txtCountry1] OR [forms]![EZ_Query_Search]![txtCountry2]

    and if you don't want to enter a country, so all countries are returned


    [forms]![EZ_Query_Search]![txtCountry1] OR [forms]![EZ_Query_Search]![txtCountry2] OR ([forms]![EZ_Query_Search]![txtCountry1] is null AND [forms]![EZ_Query_Search]![txtCountry2] is null)

  14. #14
    zashaikh is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2017
    Posts
    182
    thank you!!!!!

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

Similar Threads

  1. Replies: 2
    Last Post: 03-08-2015, 01:47 PM
  2. Replies: 14
    Last Post: 06-13-2014, 04:29 AM
  3. Query Fields On Form
    By MFS in forum Forms
    Replies: 4
    Last Post: 03-10-2014, 04:03 PM
  4. PK of fields in a form that is sourced by a query
    By accessuser1023 in forum Programming
    Replies: 3
    Last Post: 12-29-2012, 04:55 PM
  5. Using form fields as query criteria
    By bener in forum Queries
    Replies: 3
    Last Post: 10-28-2011, 12:54 PM

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