Results 1 to 7 of 7
  1. #1
    dccjr3927 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2019
    Posts
    130

    Modifying Query from Query Wizard

    I have created the following query with the wizard, but need to modify it for use in VBA.

    I have:
    Code:
    SELECT DISTINCTROW Format$([WOTracking].[Date_Time],'Long Date') AS [Date_Time By Day], WOTracking.ContractCo, Sum(WOTracking.SKUQty) AS [Sum Of SKUQty]
    FROM WOTracking 
    GROUP BY Format$([WOTracking].[Date_Time],'Long Date'), WOTracking.ContractCo;
    However I would like to limit this with the following:


    Code:
    WHERE Company = '" & Me.cbxReportCriteria & "' AND ShipDate >= #" & Format(dt1, "yyyy-mm-dd") & "# AND ShipDate <= #" & Format(dt2, "yyyy-mm-dd") & "#;
    I tried simply inserting it as follows:

    Code:
    SELECT DISTINCTROW Format$([WOTracking].[Date_Time],'Long Date') AS [Date_Time By Day], WOTracking.ContractCo, Sum(WOTracking.SKUQty) AS [Sum Of SKUQty]From WOTracking WHERE ContractCo = '" & Me.cbxReportCriteria & "' AND Date_Time >= #" & Format(dt1, "yyyy-mm-dd") & "# AND Date_Time <= #" & Format(dt2, "yyyy-mm-dd") & "# GROUP BY Format$([WOTracking].[Date_Time],'Long Date'), WOTracking.ContractCo;"
    I keep getting RunTime 3122 'query does not contain '[WOTracking].[Date_Time] as part of the aggregate function'.

    Can someone point out what I am missing?

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    don't use VBA, modify the query in query design.
    that way there's no error.
    (but date format for a date field is mm/dd/yyyy, even tho you don't want to see it that way)

  3. #3
    dccjr3927 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2019
    Posts
    130
    I am running the report from a button after selections are made on the form (start date, end date, and contract company). I am not sure how to pass the values from the form to the query.

  4. #4
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,977
    For a start, add a space before 'FROM WOTracking'.
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  5. #5
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    Consider basing the report on a simple Select query without a WHERE clause and then applying a filter to the report when you code to open the report. Use the grouping, summing and sorting features of the report instead of trying to do it in a Totals query. Or start over again with the wizard and apply the Where criteria. Or use query design rather than just trying to append a Where clause. That approach fails because this is a Totals query - you just can't append a normal Where clause to what you have.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  6. #6
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,977
    I would agree about using the query designer to get this working. You can easily convert back to SQL afterwards.

    Just to clarify, you can use either HAVING or WHERE as filters in an aggregate (totals) query.
    In fact its possible to do both at the same time...
    The difference is that HAVNG is applied before the grouping is done, WHERE afterwards.

    Both work but using HAVING is faster if the search fields are indexed. The speeds are similar if indexes aren't used.
    For examples, see http://www.mendipdatasystems.co.uk/s...s-4/4594459445 and http://www.mendipdatasystems.co.uk/s...s-8/4594556613
    Last edited by isladogs; 12-24-2019 at 08:44 AM.
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  7. #7
    dccjr3927 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2019
    Posts
    130
    Thanks guys. I got side tracked, but will try the suggestions this week. I really appreciate all of the assistance.

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

Similar Threads

  1. Modifying export query
    By ittechguy in forum Queries
    Replies: 1
    Last Post: 11-09-2015, 11:35 PM
  2. help! need help modifying a query
    By BigDan in forum Access
    Replies: 16
    Last Post: 05-28-2013, 03:58 PM
  3. Need help modifying query
    By redwagontoy in forum Queries
    Replies: 13
    Last Post: 04-30-2013, 03:25 PM
  4. Need help Modifying a query expression
    By leslina76 in forum Queries
    Replies: 3
    Last Post: 04-02-2011, 08:45 AM
  5. Modifying Update Query
    By James Elvin in forum Queries
    Replies: 0
    Last Post: 10-14-2008, 09:07 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