DeveloperBarn Forums

DeveloperBarn

Programming & IT forum

Is there an easier way to create a countif function?

This is a discussion on Is there an easier way to create a countif function? within the Visual Basic Programming forums, part of the Programming & Scripting category; I created a function that will determine whether an item should be counted based on the parameters entered. It returns ...

Go Back   DeveloperBarn Forums > Programming & Scripting > Visual Basic Programming

  #1  
Old August 7th, 2008, 06:26 PM
Lauramc's Avatar
I like Data Cubes too...
 
Join Date: Mar 2008
Real name: Laura
Location: Far Far Away
Posts: 130
Rep Power: 3
Lauramc will become famous soon enoughLauramc will become famous soon enough
Default Is there an easier way to create a countif function?

I created a function that will determine whether an item should be counted based on the parameters entered. It returns a 1 if the criteria is met and a 0 if it is not. Basically it is similar to the COUNTIF in Excel, but requires an operator ( = > < <= >= <> ). It evaluates one set of criteria, because in Reporting Services, the functions are exectued once for each row in the result set.

The main use for this is in reporting services which does not have a function like this. It is written in VB, and I am wondering if anyone can think of how this might be done more easily. Do any of you have a similar function?

See code below:
Code:
Public Function CountIF(ByVal myExpression As Object, ByVal myOperator As Object, ByVal myCondition As Object) As Integer
        Dim shouldCount As Boolean
        Dim Count As Integer
        Select Case myOperator
            Case "="
                shouldCount = (myExpression = myCondition)
            Case "<"
                shouldCount = (myExpression < myCondition)
            Case "<="
                shouldCount = (myExpression <= myCondition)
            Case ">"
                shouldCount = (myExpression > myCondition)
            Case ">="
                shouldCount = (myExpression >= myCondition)
            Case "<>"
                shouldCount = (myExpression <> myCondition)
        End Select

        If shouldCount = True Then
            Count = 1
        Else : Count = 0
        End If

        Return Count

End Function
This could be called like so (in Reporting Services) and it does work:
=Code.CountIF(Fields!MyField.Value, "<>", 0)
__________________
"The Enrichment Center is required to remind you that first you will be baked, then there will be cake." - GLaDOS

Last edited by Lauramc; August 7th, 2008 at 06:41 PM.
Reply With Quote
Reply

  DeveloperBarn Forums > Programming & Scripting > Visual Basic Programming

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads

Thread Thread Starter Forum Replies Last Post
Create History Table / Outcome results Rebelle Database Design Help 11 June 10th, 2009 10:54 AM
Create ToolTip for GridView Header jmurrayhead .Net Code Samples 0 March 21st, 2008 02:26 PM
Create Virtual Directories in IIS jmurrayhead Microsoft IIS 0 March 21st, 2008 10:08 AM


All times are GMT -4. The time now is 06:37 PM.


Copyright ©2008-2010, DeveloperBarn

Content Relevant URLs by vBSEO 3.3.2