Using Count and IIf expression in a report control box

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Bernice J
    New Member
    • Mar 2008
    • 13

    Using Count and IIf expression in a report control box

    I'm trying to count the number of records in a table with the field STATUS that have a Y value. I'm using the expression "=Count(IIf ([Status]="Y",1,0))" . It counts all the values not just the Y's. Any ideas where I'm going wrong? The field has text values in the table.
    I'm using this expression in a textbox in a report.
    Last edited by Bernice J; Mar 20 '08, 01:44 PM. Reason: Forgot a detail
  • Scott Price
    Recognized Expert Top Contributor
    • Jul 2007
    • 1384

    #2
    Why don't you try the DCount() function?

    Code:
    =DCount("[Status]", "[YourTableName]", "[Status] = 'Y'")
    Regards,
    Scott

    Comment

    • Bernice J
      New Member
      • Mar 2008
      • 13

      #3
      Originally posted by Bernice J
      I'm trying to count the number of records in a table with the field STATUS that have a Y value. I'm using the expression
      Code:
      =Count(IIf([Status]="Y",1,0))
      . It counts all the values not just the Y's. Any ideas where I'm going wrong? The field has text values in the table.
      I'm using this expression in a textbox in a report.
      I realize this has something to do with COUNT counting all records even if Null. I tried
      Code:
       =DCount([SubDate],"OpPlan","[Status]='Y'")
      and this worked better. I have this in a control box in a footer for the Group District. It is giving me a total for all records but what I really wanted is to have the total by district. I know I must have to put a reference to that in the expression but I'm unsure how. Maybe if I keep talking to myself in this forum I'll figure it out. :-)

      Comment

      • Bernice J
        New Member
        • Mar 2008
        • 13

        #4
        Originally posted by Scott Price
        Why don't you try the DCount() function?

        Code:
        =DCount("[Status]", "[YourTableName]", "[Status] = 'Y'")
        Regards,
        Scott
        Sorry, I didn't see your reply when I replied to myself. Any suggestions on the second part of my own reply?

        Comment

        • Scott Price
          Recognized Expert Top Contributor
          • Jul 2007
          • 1384

          #5
          The portion after the third comma in the DCount() expression works just like the WHERE clause of a SELECT query, without the WHERE keyword. Thus you can just add another criteria in addition the = 'Y', something like
          Code:
          AND [District] = [SomeValue]
          Regards,
          Scott

          Comment

          • Bernice J
            New Member
            • Mar 2008
            • 13

            #6
            Originally posted by Scott Price
            The portion after the third comma in the DCount() expression works just like the WHERE clause of a SELECT query, without the WHERE keyword. Thus you can just add another criteria in addition the = 'Y', something like
            Code:
            AND [District] = [SomeValue]
            Regards,
            Scott

            I have this code in the District footer because I want the total count by District. If I put in the code you suggested I get a count of ALL my records not just those for the Group. Any other ideas?

            Comment

            • Scott Price
              Recognized Expert Top Contributor
              • Jul 2007
              • 1384

              #7
              I find it a little hard to believe that further restriction in the WHERE section of DCount() results in LESS restriction!

              Please post the code that is giving you ALL records.

              Regards,
              Scott

              Comment

              • Bernice J
                New Member
                • Mar 2008
                • 13

                #8
                Here are 3 lines of code I've been trying. The first two are similar but give the same value. I used a wildcard for the District because I have more than one value for the District field. I do realize this is probably messing things up but I'm in the dark what to do and I just keep reading and testing.

                The third one is code I found in a post with a similar problem. I don't understand it obviously because I get a parameter error for "txtDistric t" and I thought the txt just meant it would give the string District.

                Code:
                =DCount("[SubDate]","OperationPlan","[Status]='Y' " And " [District]=[*]")
                
                =DCount("[SubDate]","OperationPlan","[Status]='Y' " And " [District]=' '")
                
                =DCount("[SubDate]","OperationPlan","(([District]=' "&[txtDistrict]&" ')AND ([Status]='Y'))")
                What I'm trying to do is count all the records with a submission date (SubDate) that also have a Status =Y and get a total by the group District. I have the control box in the District footer.

                Comment

                • Scott Price
                  Recognized Expert Top Contributor
                  • Jul 2007
                  • 1384

                  #9
                  In the detail section (or somewhere) of your report you should have a text box that shows what district the results are for. Set the reference to the name of this text box. The third example you showed is close to what it will look like.

                  Example:

                  Code:
                  AND [District] = " & Me.[TextBoxName] & "
                  Make sure, of course that [District] reflects the name of the column in your query resultset, and [TextBoxName] reflects the name of the text box on your report.

                  Regards,
                  Scott

                  Comment

                  • Bernice J
                    New Member
                    • Mar 2008
                    • 13

                    #10
                    Thanks Scott, it's time for a long weekend so I'll let you know how it turns out.

                    Comment

                    • Scott Price
                      Recognized Expert Top Contributor
                      • Jul 2007
                      • 1384

                      #11
                      Have a good weekend!

                      Good luck with it...

                      Regards,
                      Scott

                      Comment

                      • Bernice J
                        New Member
                        • Mar 2008
                        • 13

                        #12
                        Originally posted by Scott Price
                        Have a good weekend!

                        Good luck with it...

                        Regards,
                        Scott

                        I'm not having much luck. It doesn't recognize "Me" and I notice the Expression builder is either adding square brackets to the "Me" or removing the square brackets from the "TextBoxNam e" when I save it.

                        Comment

                        • Scott Price
                          Recognized Expert Top Contributor
                          • Jul 2007
                          • 1384

                          #13
                          Please post the code you have now that isn't working :-)

                          Thanks!

                          Regards,
                          Scott

                          Comment

                          • Bernice J
                            New Member
                            • Mar 2008
                            • 13

                            #14
                            Originally posted by Scott Price
                            Please post the code you have now that isn't working :-)

                            Thanks!

                            Regards,
                            Scott

                            If I try this I get an error#

                            Code:
                            =DCount("[SubDate]","OperationPlan","(([Status]='Y') AND ([District]= " & [txtDistrict] & ")")
                            If I try this Access automatically puts square brackets around "Me" and then shows a parameter error when I try to preview the report.
                            Code:
                            =DCount("[SubDate]","OperationPlan","(([Status]='Y') AND ([District]= " & Me.[txtDistrict] & ")")
                            I am able to get close to what I need by adding to the District Footer one textbox code
                            Code:
                            =Sum([STATUS]='Y')
                            and another with
                            Code:
                            =DCount("[SubDate]","OperationPlan","[Status]='Y' " And " [District]=[*]")
                            Then I divide the two to get the % using
                            Code:
                            =([txtSumofStatusY])/([txtDCountSubdateStatusY])
                            However for some reason I am getting a negative value for the first textbox and this carries through to the %. Another issue is that SubDate has some null values and I don't want the Status counted for those null values.
                            In another posting I explain the same problem but I used a query to select out everything. I couldn't get it to do a percent based on the total records in the datebase so I tho't I'd try doing it by going directly to the report. The more I do this the more confused I get.

                            Comment

                            • Bernice J
                              New Member
                              • Mar 2008
                              • 13

                              #15
                              Originally posted by Bernice J
                              If I try this I get an error#

                              Code:
                              =DCount("[SubDate]","OperationPlan","(([Status]='Y') AND ([District]= " & [txtDistrict] & ")")
                              If I try this Access automatically puts square brackets around "Me" and then shows a parameter error when I try to preview the report.
                              Code:
                              =DCount("[SubDate]","OperationPlan","(([Status]='Y') AND ([District]= " & Me.[txtDistrict] & ")")
                              I am able to get close to what I need by adding to the District Footer one textbox code
                              Code:
                              =Sum([STATUS]='Y')
                              and another with
                              Code:
                              =DCount("[SubDate]","OperationPlan","[Status]='Y' " And " [District]=[*]")
                              Then I divide the two to get the % using
                              Code:
                              =([txtSumofStatusY])/([txtDCountSubdateStatusY])
                              However for some reason I am getting a negative value for the first textbox and this carries through to the %. Another issue is that SubDate has some null values and I don't want the Status counted for those null values.
                              In another posting I explain the same problem but I used a query to select out everything. I couldn't get it to do a percent based on the total records in the datebase so I tho't I'd try doing it by going directly to the report. The more I do this the more confused I get.
                              I was able to figure out the SubDate null values using the code
                              Code:
                              =Sum(([STATUS]='Y') And ([SUBDATE] Is Not Null))
                              Now I just need to figure out why the values are negative and I should be done.

                              Comment

                              Working...