How to Fix Access Aggregate Function Errors with IIf and Sum
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.

- 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 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.
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.
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.
Save and close this first query so it can be referenced as an independent data source.
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.

Include Non-Aggregate Expressions in the GROUP BY Clause
Ensure that any field in your SELECT statement that isn't using an aggregate function is properly grouped.
Fix Syntax and Bracket Formatting
Prevent expression parsing errors by properly formatting field names that contain special characters or spaces.
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.

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.




