How to count values in a table

nervespy

New Member
Joined
Dec 21, 2004
Messages
2
Hello

I have a table in MS Access with 2 Columns
Column 1: Name
Column 2: Count

i.e.

Name Count
Tim 0
Tim 0
Tim 0
Bob 0
Tim 0
Bob 0

I want VB to run through the Name column and work out how many times each repeated value i.e. Bob or Tim occurs.
Then input that frequeny number in the Count column for each given value.

i.e.

Name Count
Tim 4
Tim 4
Tim 4
Bob 2
Tim 4
Bob 2

I think I will need to use an array to hold and calculate the data, then put the data back into the table from the array, but my knowledge is limited.
Does anyone know how to do this?
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
You could do it with queries.

The first query needed would be to count the occurences:

qryCount

SELECT MyTable.Name, Count(MyTable.Name) AS CountOfName INTO tblCount
FROM MyTable
GROUP BY MyTable.Name;

This will create a table with the counts.

This query would then update the original table:

qryUpdate

UPDATE tblCount INNER JOIN MyTable ON tblCount.Name = MyTable.Name SET MyTable.[Count] = [CountOfName];
 
Upvote 0
Try the following SQL:

SELECT tblName.name, DCount("[name]","tblName","[name]= '" & [name] & "'") AS ct
FROM tblName;

Replace "tblName" with the name of your table, and replace "name" with the name of the field that hold the name.

This is a dynamic query, which is better than storing the counts in a table. It is dangerous to store calculated values in a table since there is no way to "capture" a user changing a precedant value, therefor you never know if your values are current.
 
Upvote 0

Forum statistics

Threads
1,221,834
Messages
6,162,268
Members
451,758
Latest member
lmcquade91

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top