logo
search
Others

How to Fix Access Aggregate Function Errors with IIf and Sum

Nimra MalikNimra Malik Sep 30, 2026 868 views

Question details

The user is trying to total insurance units by patient and classify coverage but encounters an error stating that an IIf expression is not part of an aggregate function.

How to Fix Access Aggregate Function Errors with IIf and Sum
Product
Microsoft Access
Device & OS
not provided
Scenario
Running a query to calculate aggregate totals (Sum) and classify the data using an IIf expression at the same time.
Observed behavior
Microsoft Access prevents the query from running, reporting that the IIf expression is not part of an aggregate function.
Before you start

Before modifying your query, make a copy of your original query design and verify that all field names used in your expressions are spelled exactly as they appear in your tables.

Solution 1Recommended

Use Nested Queries to Evaluate Aggregate Aliases

Separate the grouping and conditional logic into two different queries, because Access does not allow an aggregate alias to be referenced by another expression at the same query level.

When you create a calculated field like Sum(Units) AS SumOfUnits, Microsoft Access processes the SELECT clause simultaneously. Because of this, you cannot use an IIf statement to check SumOfUnits within the same query.

1
Create the first grouped query

Open Query Design and build a base query that calculates the sum for each patient and insurance using the Sum() function. Name this aggregate alias clearly, such as SumOfUnits.

2
Save the base query

Save and close this first query so it can be referenced as an independent data source.

3
Create the second query

Start a new query and add the first query as your data source. You can now safely write your IIf expression to evaluate the SumOfUnits total and compare it with the patient's maximum total.

Use Nested Queries to Evaluate Aggregate Aliases
Avoid DMax on Base Tables: Do not use the DMax function against the base table to retrieve an aggregate alias. The alias exists only in the query results, not as a physical field in the original table.
Free Microsoft Office alternative

Need a lightweight suite to analyze your database exports? Try WPS Office

While Microsoft Access handles complex relational databases, you frequently need to analyze exported query results, build summary reports, or present your data. WPS Office is a free, lightweight, and highly compatible alternative to Microsoft Office that includes powerful Spreadsheets, Writer, and Presentation tools to handle your exported data seamlessly.

Highly compatible with Microsoft Excel (.xlsx) formats for analyzing exported Access data.Advanced Spreadsheet formulas and PivotTables to summarize database queries.Free, lightweight, and fast-loading on Windows, Mac, Linux, iOS, and Android.Intuitive tabbed interface that feels familiar and requires no learning curve.
microsoft office alternative - wps office

Frequently Asked Questions

Why does Access say my expression is not part of an aggregate function?

This error occurs when you include a field in your query's SELECT statement that is neither part of an aggregate function (like Sum, Count, or Avg) nor included in the GROUP BY clause. Access needs to know how to roll up every field in a Totals query.

Can I use an aggregate alias inside an IIf statement in the same query?

No, Microsoft Access does not allow an aggregate alias (such as a newly calculated Sum field) to be referenced by another expression at the exact same query level. You must create a primary query to calculate the aggregate, and a secondary query to evaluate it with IIf.

What does the Nz function do in an Access query?

The Nz function evaluates a variable and returns a specified value (such as zero or a specific text string like "Self-Pay") if the variable is null. This is highly useful for preventing errors in aggregate calculations or conditional IIf statements where data might be missing.