473,486 Members | 2,127 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Counting Problem

hi everybody-

i'm having a counting problem i hope you guys and gals could give me
some help with. i have a query that retrieves a bevy of information
from several different tables.

first let me give you a little background. this database is kind of
like a human resources database. it collects info about people, where
they work, and if they have any work-related issues where they work.

i have a table with people info in it, i.e. name, address, etc... i
have another table including info about where they work, i.e. division
name, address, telephone, etc...

each division is part of a bigger company. for example, "x" is a
division of company "y." each company can have many different
divisions. for example, "company y" can have division a, division b,
and division x.

here are some sample examples of my tables:

Table Person
id = primary key
last name
first name
etc...

Table Company
id = primary key; number; foreign key in Division table
name = company name

Table Division
id = primary key; number
name = name of division
companyid = foreign key to Company table
Company A has these three divisions: gas, oil, rubber
Company B has four divisions: parts, tires, motors, glass

right now my so-called "Division" table has 3 fields: id, division,
company.

id division company
1 gas Company A
2 oil Company A
3 rubber Company A
3 parts Company B
4 tires Company B
5 motors Company B
6 glass Company B

etc....
i'm running a query for a report that 1st groups Companies, then their
divisions, and then the people in each division. i'm getting some of
the results i want, but when i want to count the number of Companies,
i'm getting all of them that return in the query, not just the total
of group headings.

for example, here is what my report looks like:
Company A (1st grouping level based on "company" field from my table)
Gas (2nd grouping level based on "division" from the same table)
John Doe (from a related table in the query)
James Brown
Oil
Mindy Franlin
Taylor Dent
John Hamilton
Rubber
Ellen March

Company B
Tires
Wendy Morris
Danny Bonaduci
Guy Pierce
Motors
Jill Johnson
Mike Jones
Glass
Andy Moore
Tom Reese

etc...

in the report footer, i use the count function to count the number of
different companies. i get 6 for the number of companies. i'm using a
text box with the control source set to: "=Count([company])" without
the quotes. i'm not using "=Count(*)"

3 from Company A because it is adding: Gas, Oil, and Rubber.
3 from Company B beczause it is adding: Ties, Motors, and Glass.

i tried putting the count function in the Company footer, but it is
still counting the number of divisions. for example. i'm getting a
total of 3 in the "Company A" group footer and 3 in the "Company B"
group footer. i know why this is happening, but i don't know how to
only count each Company once.

I have my tables set up this way to separate the companies from their
division because i need to know which division is part of which
company.

how can i count the number of distict companies that are retrieved in
my query without counting the total number of times a company is
returned? does that make sense? i want the total to come out to "2"
(once for Company A and once for Company B) instead of 6. is there a
keyword like "distinct" i should use? or should i go back to the
query, add the "company" field a qnd time, then use one of the totals
functions?

i haven't tried any other counts yet, i.e. the total number of people
in each division and then the total number of people in each company,
but i'm hoping they'll work.

thanks for reading my long post! thans in advance for your help/
advice! i truly appreciate it!

take care,

megan
Nov 13 '05 #1
3 1950
me**************@hotmail.com (Megan) wrote in message news:<5c**************************@posting.google. com>...
hi everybody-

i'm having a counting problem i hope you guys and gals could give me
some help with. i have a query that retrieves a bevy of information
from several different tables.


Megan,

You did a great job explaining what your problem is. I checked
reports I created in the past that grouped on multiple fields and I'm
not happy with what I did in those cases. When I couldn't use SUM or
COUNT directly I created Public functions in a module that would
ensure I got the total or count that I wanted. For example:

Control Source for the txtNumberOfCompanies textbox in the Footer:

=CountSQLRecords("SELECT CompanyName FROM ... GROUP BY CompanyName;")

Note that the SQL string used to get the count is somewhat similar to
the one used for the RecordSource except you are now free to group the
same way the report groups in order to get your count. You can also
test out your SQL strings first by saving your report's RecordSource
as a query and making sure the groupings give you what you want. I
look forward to seeing if others have solved this problem so that I
can simplify my reports. P.S., using 'Name' as the name of a field
could possibly confuse Access.

James A. Fortune
Nov 13 '05 #2
ja******@oakland.edu (James Fortune) wrote in message news:<a6**************************@posting.google. com>...
me**************@hotmail.com (Megan) wrote in message news:<5c**************************@posting.google. com>...
hi everybody-

i'm having a counting problem i hope you guys and gals could give me
some help with. i have a query that retrieves a bevy of information
from several different tables.


Megan,

You did a great job explaining what your problem is. I checked
reports I created in the past that grouped on multiple fields and I'm
not happy with what I did in those cases. When I couldn't use SUM or
COUNT directly I created Public functions in a module that would
ensure I got the total or count that I wanted. For example:

Control Source for the txtNumberOfCompanies textbox in the Footer:

=CountSQLRecords("SELECT CompanyName FROM ... GROUP BY CompanyName;")

Note that the SQL string used to get the count is somewhat similar to
the one used for the RecordSource except you are now free to group the
same way the report groups in order to get your count. You can also
test out your SQL strings first by saving your report's RecordSource
as a query and making sure the groupings give you what you want. I
look forward to seeing if others have solved this problem so that I
can simplify my reports. P.S., using 'Name' as the name of a field
could possibly confuse Access.

James A. Fortune

Thanks for the help James!!!

I haven't tried your idea yet, but I came up with another idea. I put
a text box with "Name = RecordCount" (without the quotes) in the
"Companies" Group Heading with the Control Source = 1. Then, I put
another text box in the "Companies" Group Footer, with the Control
Source = [RecordCount] and it gave me the total I needed.

I didn't know you could use SQL with a text box's control source.
Cool! There's a lot of things I'd like to try now!

P.S. Thanks for the compliment about doing a good job explaining what
I'm trying to do! I always try to be as clear and detailed as possible
while still trying to be as concise as possible. I read a lot of your
posts even if they don't apply to me in order to learn something new
or an alternate way to accomplish something. Thanks again! Take it
easy!

Megan
Nov 13 '05 #3
me**************@hotmail.com (Megan) wrote in message news:<5c**************************@posting.google. com>...
Thanks for the help James!!!

I haven't tried your idea yet, but I came up with another idea. I put
a text box with "Name = RecordCount" (without the quotes) in the
"Companies" Group Heading with the Control Source = 1. Then, I put
another text box in the "Companies" Group Footer, with the Control
Source = [RecordCount] and it gave me the total I needed.

I didn't know you could use SQL with a text box's control source.
Cool! There's a lot of things I'd like to try now!

P.S. Thanks for the compliment about doing a good job explaining what
I'm trying to do! I always try to be as clear and detailed as possible
while still trying to be as concise as possible. I read a lot of your
posts even if they don't apply to me in order to learn something new
or an alternate way to accomplish something. Thanks again! Take it
easy!

Megan


I guess I need to compliment you on your resourcefulness. It's
important to keep simplifying things without losing the essentials. I
learned something new from you also. Thanks for your encouragement.

James A. Fortune
Nov 13 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2162
by: Elbert Lev | last post by:
Please correct me if I'm wrong. Python (as I understand) uses reference counting to determine when to delete the object. As soon as the object goes out of the scope it is deleted. Python does...
0
1736
by: Kalle Rutanen | last post by:
Hello I implemented reference counting in my program, and found out many problems associated with it. I wonder if the following problems can be solved automatically rather manually ? 1. ...
1
3224
by: Tony Johansson | last post by:
Hello Experts! I reading a book called programming with design pattern revealed by Tomasz Muldner and here I read something that I don't understand completely. It says "A garbarage...
18
2915
by: ChadDiesel | last post by:
I appreciate the help on this group. I know I've posted a lot here the last couple of weeks, but I was thrown into a database project at my work with very little Access experience. No other...
1
6906
by: j | last post by:
Hi, I've been trying to do line/character counts on documents that are being uploaded. As well as the "counting" I also have to remove certain sections from the file. So, firstly I was working...
4
4174
by: aaronfude | last post by:
Hi, Please consider the following class (it's not really my class, but it's a good example for my question): class Vector { int myN; double *myX; Vector(int n) : myN(n), myX(new double) { }...
10
1490
by: cj | last post by:
I'm writing a TCP/IP server app that will have many simultaneous connections. The main thread listens for new connections and starts a thread to handle each requested connection. These are short...
4
1908
by: Dado | last post by:
I have a next situation with the textbox field: A - B = C 1. How to fill the A fill with the data from my previous recordset ? Can I do it with the expression builder ? 2. I want that every...
4
8419
by: bigbagy | last post by:
Notes The programs will be compiled and tested on the machine which runs the Linux operating system. V3.4 of the GNU C/C++ compiler (gcc ,g++) must be used. A significant amount coding is...
0
6964
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7126
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6842
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7330
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5434
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4559
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3070
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3070
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1378
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.