count 1s in access 2016

I have a query that tells me where the data has changed over a period the fields represent the data element

the last column gives me the representation of what changed. I would also like a numerical count so i can sort by number of changes.

so 10000000 =1
      00100111=4


and so on but not sure how to do that, i wonder if maybe regular expressions can do that or maybe a function

SELECT TblUnionCodes.Capcode, Count(QryLongModelDescEvents.CapCode) AS LongModelDesc, Count(QryManfEvents.CapCode) AS Manf, Count(QryNomEvents.CapCode) AS Nom, Count(QrySeatsEvents.CapCode) AS Seats, Count(QryShortModelDescEvents.CapCode) AS ShortModelDesc, Count(QryTransmissionEvents.CapCode) AS Transmission, Count(QryValvesEvents.CapCode) AS Valves, Count(QryWBaseTypeEvents.CapCode) AS WBaseType, IIf(Count([QryLongModelDescEvents].[capcode])>0,"1",0) & IIf(Count([QryManfEvents].[capcode])>0,"1",0) & IIf(Count([QryNomEvents].[capcode])>0,"1",0) & IIf(Count([QrySeatsEvents].[capcode])>0,"1",0) & IIf(Count([QryShortModelDescEvents].[capcode])>0,"1",0) & IIf(Count([QryTransmissionEvents].[capcode])>0,"1",0) & IIf(Count([QryValvesEvents].[capcode])>0,"1",0) & IIf(Count([QryWBaseTypeEvents].[capcode])>0,"1",0) AS ResultString
FROM (((((((TblUnionCodes LEFT JOIN QryLongModelDescEvents ON TblUnionCodes.Capcode = QryLongModelDescEvents.CapCode) LEFT JOIN QryManfEvents ON TblUnionCodes.Capcode = QryManfEvents.CapCode) LEFT JOIN QryNomEvents ON TblUnionCodes.Capcode = QryNomEvents.CapCode) LEFT JOIN QrySeatsEvents ON TblUnionCodes.Capcode = QrySeatsEvents.CapCode) LEFT JOIN QryShortModelDescEvents ON TblUnionCodes.Capcode = QryShortModelDescEvents.CapCode) LEFT JOIN QryTransmissionEvents ON TblUnionCodes.Capcode = QryTransmissionEvents.CapCode) LEFT JOIN QryValvesEvents ON TblUnionCodes.Capcode = QryValvesEvents.CapCode) LEFT JOIN QryWBaseTypeEvents ON TblUnionCodes.Capcode = QryWBaseTypeEvents.CapCode
GROUP BY TblUnionCodes.Capcode
HAVING (((Count(QryLongModelDescEvents.CapCode))>0)) OR (((Count(QryManfEvents.CapCode))>0)) OR (((Count(QryNomEvents.CapCode))>0)) OR (((Count(QrySeatsEvents.CapCode))>0)) OR (((Count(QryShortModelDescEvents.CapCode))>0)) OR (((Count(QryTransmissionEvents.CapCode))>0)) OR (((Count(QryValvesEvents.CapCode))>0)) OR (((Count(QryWBaseTypeEvents.CapCode))>0))
ORDER BY IIf(Count([QryLongModelDescEvents].[capcode])>0,"1",0) & IIf(Count([QryManfEvents].[capcode])>0,"1",0) & IIf(Count([QryNomEvents].[capcode])>0,"1",0) & IIf(Count([QrySeatsEvents].[capcode])>0,"1",0) & IIf(Count([QryShortModelDescEvents].[capcode])>0,"1",0) & IIf(Count([QryTransmissionEvents].[capcode])>0,"1",0) & IIf(Count([QryValvesEvents].[capcode])>0,"1",0) & IIf(Count([QryWBaseTypeEvents].[capcode])>0,"1",0) DESC;

Open in new window

ee.PNG
PeterBaileyUkAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim Dettman (Microsoft MVP/ EE MVE)President / OwnerCommented:
Just a function to loop through the string and count the 1's

Jim.
Rgonzo1971Commented:
Hi,

pls try something like this

Len([ResultString])-Len(Replace([ResultString],"1",""))

Open in new window

Regards
Bill PrewIT / Software Engineering ConsultantCommented:
Couldn't this just be:

Len(Replace([ResultString],"0",""))


~bp

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Your Guide to Achieving IT Business Success

The IT Service Excellence Tool Kit has best practices to keep your clients happy and business booming. Inside, you’ll find everything you need to increase client satisfaction and retention, become more competitive, and increase your overall success.

PeterBaileyUkAuthor Commented:
Thank you was a very good solution with minimal coding needed.
Bill PrewIT / Software Engineering ConsultantCommented:
Welcome.

~bp
PatHartmanCommented:
Just FYI, the aggregation functions like Count(), Avg(), Sum() aggregate over the entire recordset so  having multiple Count() functions doesn't really make sense unless what you are really counting is non-null values.

Count(*) counts the rows selected
Count(somefield) counts the non-null values of somefield for the rows selected.  If all rows are populated, the two counts would return the same number.

Also, keep in mind that the query engine can optimize Count(*) but it cannot optimize Count(somefield).  The second expression will most likely force a full table scan whereas the first can frequently use indexes.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.