Solved

In an Access 2003 application, how to resolve the error "You tried to execute a query that does not include the specified expression ... as part of an aggregate function?

Posted on 2014-02-10
2
1,777 Views
Last Modified: 2014-02-10
In an Access 2003 application, how to fix the error
"You tried to execute a query that does not include the specified expression
'IIF(tbl_SMR_OpenItems.T="E","D",tbl_SMR_OpenItems.T)' as part of an
aggregate function.

SELECT tbl_SMR_OpenItems.Bank,
          IIf(tbl_SMR_OpenItems.T="E","D",tbl_SMR_OpenItems.T) AS T,
          IIf([AgeDays]<6,"1-5",
          IIf([AgeDays]<11,"6-10",
          IIf([AgeDays]<16,"11-15",
          IIf([AgeDays]<21,"16-20",
          IIf([AgeDays]<26,"21-25",
          IIf([AgeDays]<31,"26-30",
          "31+")))))) AS AgeRange,
          Sum(tbl_SMR_OpenItems.amount) AS [$],
          Count(tbl_SMR_OpenItems.Type) AS Items
          INTO tbl_SMR_OpenItemsAgeRange
          FROM tbl_SMR_OpenItems
          GROUP BY tbl_SMR_OpenItems.Bank,
          tbl_SMR_OpenItems.T,
          IIf([AgeDays]<6,"1-5",
          IIf([AgeDays]<11,"6-10",
          IIf([AgeDays]<16,"11-15",
          IIf([AgeDays]<21,"16-20",
          IIf([AgeDays]<26,"21-25",
          IIf([AgeDays]<31,"26-30",
          "31+"))))));
0
Comment
Question by:zimmer9
  • 2
2 Comments
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
Comment Utility
You will not be able to refer to the computed field, [T], you will need to use the expression:

    IIf(tbl_SMR_OpenItems.T="E","D",tbl_SMR_OpenItems.T)

Just like you did with the expession for AgeRange

 GROUP BY tbl_SMR_OpenItems.Bank,
 IIf(tbl_SMR_OpenItems.T="E","D",tbl_SMR_OpenItems.T),
 IIf([AgeDays]<6,"1-5",
 IIf([AgeDays]<11,"6-10",
 IIf([AgeDays]<16,"11-15",
 IIf([AgeDays]<21,"16-20",
 IIf([AgeDays]<26,"21-25",
 IIf([AgeDays]<31,"26-30",
 "31+"))))));
0
 
LVL 47

Accepted Solution

by:
Dale Fye (Access MVP) earned 500 total points
Comment Utility
You might also want to consider creating a table for those age ranges, something like: tbl_lst_AgeRanges (RangeGE, RangeLT, RangeText)  with values like:

RangeGE   RangeLT    RangeText
0                     6           "1-5"
6                    11          "6-10"
11                  16          "11-15"
16                  21          "16-20"
21                  26          "21-25"
26                 31           "26-30"
31                 999           "31+"

Then, in your query, you would use a non-equi join (can only be completed in the SQL view) like:

SELECT tbl_SMR_OpenItems.Bank, RangeText as AgeRange
FROM tbl_SMR_OpenItems
INNER JOIN tbl_lst_AgeRanges
ON tbl_SMR_OpenItems.[AgeDays] >= tbl_lst_AgeRanges.RangeGE
AND tbl_SMR_OpenItems.[AgeDays] < tbl_lst_AgeRanges.RangeLT

This way, you could easily change you age ranges in a table, and not have to go looking for where you did it in code or queries.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now