How to Return Groups with Exactly Three Values in an Access Crosstab Query
Question details
The user needs to filter groups in a Microsoft Access crosstab query to return only those containing exactly three values without triggering a syntax error.

- Product
- Microsoft Access
- Device & OS
- not provided
- Scenario
- Attempting to use an aggregate COUNT function to filter records by quantity within a TRANSFORM statement.
- Observed behavior
- Microsoft Access throws a syntax error when using the HAVING COUNT(*) clause or when trying to wrap the TRANSFORM statement in a subquery.
Ensure your underlying tables and relationships are correctly set up and identify the specific ID field you want to group and count by.
Use a Preliminary Totals Query to Pre-filter the Data
Create a separate totals query to filter the records before passing them into the crosstab query, bypassing the Access syntax limitations.
Microsoft Access does not support using the HAVING clause directly inside a TRANSFORM crosstab query. Attempting to do so, or wrapping the TRANSFORM query in a standard subquery, will typically result in a 'Syntax error in TRANSFORM statement' error.
To resolve this, you must split the operation into two distinct steps: first, create a standard totals query that filters down to the groups containing exactly three values, and second, use that query as the data source for your crosstab pivot.
Go to Create > Query Design and add your main tables. Add the target grouping ID (e.g., health_sys_id) and the field you want to count. Click the Totals button in the ribbon to group by the ID, set the count field's Total row to Count, and enter = 3 in its Criteria row. Save this query as 'qry_CountFilter'.
Create a new query and add both your original data tables and the newly saved 'qry_CountFilter'. Create an Inner Join between them using the grouping ID field so that only records belonging to the filtered groups remain.
Use this new, pre-filtered dataset as the source for your Crosstab Query. You can now safely apply your TRANSFORM and PIVOT statements to group the categories and pivot the data without encountering syntax errors.

Manage and Analyze Data Easily with WPS Office
While Microsoft Access is used for complex relational database queries, WPS Office Spreadsheets offers a lightweight, free alternative for organizing data, creating powerful pivot tables, and analyzing records without needing to write SQL queries.
- 1. Download WPS Office: Get WPS Office Free from the official website and follow the easy installation instructions.
- 2. Import Your Data: Open WPS Spreadsheets and import your exported database records or CSV files.
- 3. Create a PivotTable: Go to the Insert tab and click PivotTable to quickly build dynamic crosstab summaries without writing queries.

Frequently Asked Questions
Why do I get a syntax error when using HAVING in a TRANSFORM query?
Microsoft Access SQL engine cannot process the HAVING clause directly within a TRANSFORM statement. It struggles to resolve aggregate filtering simultaneously while pivoting the dataset, which triggers a syntax error.
Can I use a subquery to filter an Access crosstab query?
While subqueries work well in standard SELECT queries, nesting a TRANSFORM query inside a SELECT subquery or vice versa often leads to syntax errors in Microsoft Access. The safest and most reliable method is utilizing a saved preliminary query.
How do I filter an Access crosstab query by date range?
You can use a standard WHERE clause within the TRANSFORM query to filter specific attributes like date ranges (e.g., WHERE year = '2020') before the data is grouped. However, for aggregate condition filters like counts, you still must use a separate preliminary totals query.




