Results 1 to 8 of 8
  1. #1
    cowboy is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2010
    Posts
    291

    Filtering a form based on query totals query which is based on query with dates

    I have a subquery that gets all the Dates and each record is an ingredient that will be used in a recipe for example

    Code:
    Date         Amount    Ingredient
    7/1/2013      3 Cups     Flour
    7/3/2013      5 Cups     Flour
    7/12/2013     1 Cups     Flour
    Then I have a query that queries the subquery above and groups ingredients together and sums the amounts so it will have something like this

    Code:
    Ingredient       SumofAmount
    Flour               8 Cups
    Vinegar             3 Cups
    Sugar               7 Cups



    Now everything is working fine if I want to get the ingredient total for the whole database, but I would like to be able to filter by date.

    How do I filter a form based on the 2nd query from field from its subquery?

  2. #2
    Dal Jeanis is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    Assuming Q1 is your first query that returns (magically) records with the three fields (IngrDate IngrAmount IngrName), then here's what your second query Q2 looks like, approximately:
    Code:
    Q2: 
    SELECT IngrName, Sum(IngrAmount) AS SumOfAmount
    FROM Q1
    GROUP BY IngrName;
    You just need:
    Code:
    Q2B:  
    SELECT IngrName, Sum(IngrAmount) AS SumOfAmount
    FROM Q1
    WHERE IngrDate BETWEEN (date1) AND (date2)
    GROUP BY IngrName;
    So the SQL change is trivial. The exact syntax will depend on what kind of box you put on your form to select the start and end dates, and whether you want to allow someone to have only a start date, or only an end date, or whatever. if you post the exact SQL you have, both Q1 and Q2, and the exact names of the text boxes or combo boxes that have the dates, we'll help you figure it out.

    The other thing I want to verify: the word "subquery" has a specific meaning, and I think you may not mean it. A subquery is (loosely) a small section within a given query that queries a different table based on the current record of the main query.

    If what you are calling your subquery is a saved query, with a name of its own, rather than a piece of a larger query, then that particular term might get you some confusing and unhelpful responses.

  3. #3
    cowboy is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2010
    Posts
    291
    When I was using the word subquery I was referring to Q1 which would be its own query collecting the necessary data to run Q2.

    Currently my form opens using Q2 as the record source and it asks for the parameters StartDate and EndDate.
    Is there a way to open the form from another form using
    DoCmd.OpenForm
    and have it automatically fill in the StartDate and EndDate so I am not asked for them?

  4. #4
    Dal Jeanis is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    1) That's what I thought. I just wanted to point it out, because the term has a technical meaning that could be a gotcha if you asked a different question later.

    2) Yes - google "OpenArgs" The reference page for Access 2003 is http://msdn.microsoft.com/en-us/libr...ffice.11).aspx, but there are a lot of different pages on how to use it.

  5. #5
    cowboy is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2010
    Posts
    291
    If you want to use OpenArgs, you have to get the arguments out with the forms Form_Open event correct? When I open the form it asks me for the parameter variables before the Form_Open event is called.

  6. #6
    Dal Jeanis is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    Hmm. I'll have to research, but if you're right and that's the way they decide to order the events, then you have two options -

    1) Take the record source off the form, and assign the record source to the form during the form open event, after you receive the openargs;

    or

    2) Assign the desired values to temporary variables, and change the query to use the temporary variables.
    http://accessexperts.com/blog/2010/0...2007-and-2010/
    http://msdn.microsoft.com/en-us/libr.../ff194564.aspx

  7. #7
    cowboy is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2010
    Posts
    291
    Great, I had never heard of TempVars, seems just like a global variable. Thanks for the help.

  8. #8
    Dal Jeanis is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    Happy to oblige, cowboy.

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

Similar Threads

  1. Replies: 18
    Last Post: 10-10-2012, 10:10 AM
  2. Query based on dates ish
    By BigMac4 in forum Queries
    Replies: 4
    Last Post: 08-25-2012, 10:05 AM
  3. Filtering based on form dates
    By cbh35711 in forum Programming
    Replies: 3
    Last Post: 03-13-2012, 11:46 AM
  4. Totals in a query-based report
    By babylikesburgh in forum Reports
    Replies: 4
    Last Post: 02-24-2010, 03:08 PM
  5. Replies: 0
    Last Post: 07-30-2009, 12:40 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