Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 536209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:47:52+00:00 2026-05-13T09:47:52+00:00

I don’t even know if I am doing this query the right way. There

  • 0

I don’t even know if I am doing this query the right way.
There is a Sandwiches table that has some 7 fields and 2 of them are comboboxes (Type and Bread).

So I made a query that combines all of the comboboxes values into one query, like this:

SELECT TypesAndBreads.TBName, TypesAndBreads.Type
FROM (SELECT [Sandwiches Types].[Sandwich Type] As TBName, "Sandwich Type" As Type
    FROM [Sandwiches Types]
UNION ALL
    SELECT Breads.Bread As TBName, "Bread" As Type 
    FROM Breads)  AS TypesAndBreads;

I get the flat values of the tables now I want to count all the sandwiches under each TypesAndBreads.TBName. I have this, just to make sure it works with all the Sandwiches:

SELECT TypesAndBread.Type, TypesAndBread.TBName,
       (SELECT Count(Sandwiches.[SandwichID]) As SandwichCount
        FROM Sandwiches) As SandwichCount
FROM TypesAndBread;

But I want to reference the current Type and TBName inside the subquery. Something like this:

SELECT TypesAndBread.Type, TypesAndBread.TBName,
       (SELECT Count(Sandwiches.[SandwichID]) As SandwichCount
        FROM Sandwiches
        WHERE Sandwiches.[TypesAndBread.Type] = Sandwiches.[TypesAndBread.TBName]) As SandwichCount
FROM TypesAndBread;

But of course this doesn’t work. I didn’t think it will, just thought of giving it a try. I was thinking of maybe constructing the query with VBA when they open the Report that this query is going to be based of.

So I guess my question is: Is there a way to reference the current selected fields in a subquery? Or is there a different way to approach this?

Thanks for the help

EDIT:
My table structure is like this:

Sandwiches‘s fields

| SandwichID | Name | Date Added | Chef | Sandwich Type | Bread | Reviewed By |

where Sandwich Type and Bread are Lookup fields for these tables:

Sandwiches Types‘s fields

| Sandwich Type |

Breads‘s fields

| Bread |

The TypesAndBreads query combined the Sandwiches Types and Breads tables, but the reason for that is so that I can get the count of all the sandwiches that have that Type or bread. A result like this:

+=============================================+
|      Type     |    TBName   | SandwichCount |
+=============================================+
| Sandwich Type | Turkey Club |            10 |
| Bread         | Italian     |             5 |
| Bread         | Garlic      |             8 |
+---------------------------------------------+

the example result’s first row basicly says there are 10 sandwiches in record with the Sandwich Type field equal to Turkey Club.

I hope that explains it better.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T09:47:52+00:00Added an answer on May 13, 2026 at 9:47 am

    Not sure if Access supports it, but in most engines (including SQL Server) this is called a correlated subquery and works fine:

    SELECT  TypesAndBread.Type, TypesAndBread.TBName,
            (
            SELECT  Count(Sandwiches.[SandwichID]) As SandwichCount
            FROM    Sandwiches
            WHERE   (Type = 'Sandwich Type' AND Sandwiches.Type = TypesAndBread.TBName)
                    OR (Type = 'Bread' AND Sandwiches.Bread = TypesAndBread.TBName)
            ) As SandwichCount
    FROM    TypesAndBread
    

    This can be made more efficient by indexing Type and Bread and distributing the subqueries over the UNION:

    SELECT  [Sandwiches Types].[Sandwich Type] As TBName, "Sandwich Type" As Type,
            (
            SELECT  COUNT(*) As SandwichCount
            FROM    Sandwiches
            WHERE   Sandwiches.Type = [Sandwiches Types].[Sandwich Type]
            )
    FROM    [Sandwiches Types]
    UNION ALL
    SELECT  [Breads].[Bread] As TBName, "Bread" As Type,
            (
            SELECT  COUNT(*) As SandwichCount
            FROM    Sandwiches
            WHERE   Sandwiches.Bread = [Breads].[Bread]
            )
    FROM    [Breads]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Don't know if there is a better way to do this, so that is
Don't know how to google for such, but is there a way to query
Don't know if this has been asked before, so point me to another question
Don't know if anyone can help me with this or if it's even possible.
Don't know if this is the right place to ask this, but I will
Don't know if it's I'm doing it wrong or if there's a bug (I
Don't know if this has been answered before. Have custom routes to users. If
don't know if this is possible.. I'm using sqlite3 schema: CREATE TABLE docs (id
don't know what happened in my jcrop selection. I think I pressed some keys
I don't know why, but this code worked for me a month ago... maybe

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.