Results 1 to 3 of 3
  1. #1
    PCartland is offline Novice
    Windows 10 Access 2013 64bit
    Join Date
    Feb 2017
    Posts
    11

    append a record to a table when an aggregate query returns no records

    I have a table in an access 2010 dB that stores metrics that are reported monthly. I use multiple append queries that sum values or count records in other tables to populate the monthly metric table. I use a macro to run the individual queries and a form to collect the end date for the report month. I want to insert records with "0" values when the aggregate queries return no records.



    An example aggregate query is

    Code:
    INSERT INTO NewMonthlyMetric ( MetricYear, [Metric Month],Slide, Chart, Metric, MetricValue, MetricEndDate )
    SELECT DatePart("yyyy",[Forms]![MonthlyAppend]![EndDate]) AS [Metric Year], Month([Forms]![Monthly Append]![EndDate])AS [Metric Month], "CCB Actions" AS Slide, "Budget Bullets"AS Chart, "Budget Increase" AS Metric, Sum([Change RequestIntake].[Estimated Cost]) AS [SumOfEstimated Cost], [Forms]![MonthlyAppend]![EndDate] AS Expr1
    FROM [Change Request Intake]
    WHERE ((([Change Request Intake].[Change Request Status])Not Like "On Hold for Additional Information") AND (([Change RequestIntake].[For Admin Only]) Is Null) AND (([Change Request Intake].[CategoryBudget]) Is Not Null) AND (([Change Request Intake].[Decision FinalDate])>DateAdd('d',-DatePart('d',[Forms]![MonthlyAppend]![EndDate]),[Forms]![Monthly Append]![EndDate])) AND (([Change RequestIntake]![Date Submitted])<=[Forms]![Monthly Append]![EndDate]))
    HAVING (((Sum([Change Request Intake].[EstimatedCost]))<0));
    Is the best way to accomplish this to use if statements for each append query in the macro? I'm hoping there's an easier way since this would mean there would be two append queries for each aggregate query: one for the aggregate append and one for the "0" append.

    Thanks
    Paul

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,816
    Saving aggregate data is usually a bad idea. Regardless, I would not save 0 value records.

    This could get very, very complicated.

    Approach using two APPEND queries, the "0" query could be like:

    INSERT ... SELECT ... WHERE NOT someIDfield IN (SELECT someIDfield FROM ...)

    To do with only one APPEND, would first need a dataset of all possible combinations of months and metrics. This might be accomplished with a Cartesian query. This is a query with no JOIN clause so every record in each table is associated with every record in other table. The resulting dataset can be massive and slow. Then this dataset would be joined on multiple fields (compound join) with the metrics data table with a JOIN type of 'show all records from the [Cartesian query] and only those from [metrics table] that match'. Then build an APPEND query from that.

    Or build VBA procedure to write data to table.
    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
    PCartland is offline Novice
    Windows 10 Access 2013 64bit
    Join Date
    Feb 2017
    Posts
    11
    Normally I wouldn't save "0" results, but we execute another query to export records that get used to buld PowerPoint slide charts. Including records for queries that return "0" makes it easier for the person who has to create the charts. Ideally I wouldn't store "0" records but I can't include records in the reporting select query for records that don't exist.

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

Similar Threads

  1. Replies: 4
    Last Post: 07-14-2015, 06:49 PM
  2. Replies: 3
    Last Post: 05-14-2015, 04:17 PM
  3. Append Query Only Returns ZERO 0 rows
    By SealM in forum Queries
    Replies: 6
    Last Post: 02-28-2013, 01:01 PM
  4. Aggregate function returns duplicate values
    By lokiluke in forum Queries
    Replies: 3
    Last Post: 09-16-2011, 09:40 AM
  5. Aggregate Query Returns No Values
    By Xiaoding in forum Queries
    Replies: 6
    Last Post: 03-29-2010, 02:01 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