CountIf formula within MS Access report
Asked Answered
D

2

5

Something terrible as happened with my poor attempt at adding a CountIf forumula to my access report.

I am trying to add a formula within my report header to count the number of entries in my Service column that contain a certain word. The problem is I am an absolute amateur when it comes to SQL code.

=CountIf([Service]="Housing")

Is the code I was hoping would work but I don't seem to be getting anywhere.

Detumescence answered 2/7, 2012 at 14:15 Comment(2)
Surely that would only count it if it was the word "Housing" only, not if it was in it..Stillwell
well my intention is for it to count the amount of entries made for Housing, there are other services too such as Environmental Health and such. so in this example i would only want it to count housing but its not playing game. i dont understand the "Not if it was in it" part. as explained i am not very good at SQL coding and all that larkDetumescence
S
14

There is no Countif function in Access. However, you can use Sum and IIf (ie, Immediate If) to accomplish the same thing. Try:

=Sum(IIf([Service] Like "*Housing*", 1, 0))

The above assumes you want the Service column to contain the word "Housing". This assumes you were being precise in the wording of your question. If you really meant that you want to count the number of records where the Service column equals "Housing" exactly, you would use this instead:

=Sum(IIf([Service] = "Housing", 1, 0))
Spooner answered 2/7, 2012 at 14:36 Comment(2)
superb, thank you! im not sure what the etiquette is and whether i need to ask another question for this, but basically these entries on the database are for people that have made mistakes in certain services that they are trained in at my work. not everyone is trained in the same services and this is to show how many "feedback entries" there are for each person in each service. the report asks the persons name and a date period, and outputs the entries that have been made. is there a way of this only showing and populating the service that have been entered for that person?Detumescence
You can add additional criteria using AND and OR. For example, =Sum(IIf([Service] = "Housing" AND [EmployeeName] = "John")). However, since you are doing this in a report, it would be better to filter the report itself when you open it to only show certain records. Check out the help for DoCmd.OpenReport. If you still have trouble, it's probably best to ask a separate question.Spooner
R
2

This also works:

=Count(IIf([Service]="Housing",1))
Radicel answered 19/5, 2016 at 21:55 Comment(1)
Added benefit of not returning 0 when there are no matchesTremain

© 2022 - 2024 — McMap. All rights reserved.