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

Ask A Question

Stats

  • Questions 277k
  • Answers 277k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The description (text of the <option>s) isn't posted from a… May 13, 2026 at 3:05 pm
  • Editorial Team
    Editorial Team added an answer As a general practice, I would not suggest using exception… May 13, 2026 at 3:05 pm
  • Editorial Team
    Editorial Team added an answer FontSize is not a required element, and neither is RunProperties.… May 13, 2026 at 3:05 pm

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
In order to apply a triggered animation to all ToolTip s in my app,
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
I don't know when to add to a dataset a tableadapter or a query
I don't edit CSS very often, and almost every time I need to go

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.