Solved

MS Access - updating field with count of records

Posted on 2009-07-02
7
1,071 Views
Last Modified: 2013-11-27
I think I have to push this out to a temp table then update - hoping there is a more direct way...

Table looks like

ID        State     Code
1          FL          123
2          FL           123
3          FL           456
5          AL           789

I want to update the SAME table to

ID        State     Code      Count of State           Count of Code
1          FL          123           3                               2
2          FL           123          3                               2
3          FL           456          3                               1
5          AL           789          1                               1

Is there a way to do this in a query or two.  As I said above I think I need to push the counts to a temp table then update....
I'm hoping I don't have to do that.
Thanks
Keith
0
Comment
Question by:keschuster
7 Comments
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 24767009
try this query

select ID, State, Code, (select Count(*) from tablex A where A.State=Tablex.state) as CountOfState, (select Count(*) from tablex A where A.code=Tablex.code) as CountOfCode
from Tablex;
0
 

Author Comment

by:keschuster
ID: 24767092
That gives me the numbers - How do I update the two last columns.  I get the error, must use updatable query
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 24767601
keschuster said:
>>How do I update the two last columns

With respect, you shouldn't try.  Those are derived data, and as such should not be stored in the db.  Rather,
you should calculate them on demand in your queries, reports, and/or forms.
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 119

Assisted Solution

by:Rey Obrero
Rey Obrero earned 100 total points
ID: 24767635
just convert the query to a make table query


SELECT Tablex.ID, Tablex.State, Tablex.Code, (select Count(*) from tablex A where A.State=Tablex.state) AS CountOfState, (select Count(*) from tablex A where A.code=Tablex.code) AS CountOfCode INTO TableX_1
FROM Tablex;
0
 

Author Comment

by:keschuster
ID: 24767655
Cap -
Is was trying to avoid creating a new table
Is it possible to update TableX with those values?  
0
 
LVL 5

Accepted Solution

by:
Lawrence Barnes earned 400 total points
ID: 24767956
If the table is not large you could used a domain function.  In your update query you would place this in the "update to" portion.  If you have many records, then I would go with Capricorn's direction as domain functions take a long time on large tables.

* Something to consider:  Do you want the Code count to be across all states or the number of codes within the state?  I'll assume it will be the number of codes within the state for now.
* I'm also assuming that the code is a number format.

Domain function (I use if it is less than 10,000 rows.)
1) Create a new query with the table in it.  Right click on the table name in the query designer and rename it.  If the table name was [tblStateCodes], I would rename it to [aliastblStateCodes].
2) Place the code below into your update query's update to row.  (Make sure column names match.)

I've attached a sample db for you.  My guess is that Capricorn showed me this a few years ago in one of his posts.

Good luck,

LVBarnes
tblStateCodes

ID Autonumber

State Text

Code Long Integer
 

CountOfState: DCount("State","tblStateCodes","State='" & [aliastblStateCodes].[State] & "'")
 

CountOfCode: DCount("Code","tblStateCodes","State='" & [aliastblStateCodes].[State] & "' AND Code=" & [aliastblStateCodes].[Code])

Open in new window

Counts.mdb
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 24770671
keschuster,

<Is it possible to update TableX with those values?  >
Again, as stated eariler, you really do not want to "Store" calculated data, except in rare circumstances.

Just FYI

JeffCoachman
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

911 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

22 Experts available now in Live!

Get 1:1 Help Now