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

Sponsored Links

Discuss "Is there an easier way to create a countif function?" in the Visual Basic Programming forum.

Visual Basic Programming - Learn coding practices and tips to get the best out of your Visual Basic applications. Visual Basic was developed by Microsoft for building stand alone Windows based applications.


Reply « Previous Thread | Next Thread »  
 
LinkBack Thread Tools Display Modes
  #1  
Old August 7th, 2008, 06:26 PM
Lauramc's Avatar
I like Data Cubes too...

 
Join Date: Mar 2008
Location: Far Far Away
Posts: 47
Thanks: 1
Thanked 4 Times in 2 Posts
Rep Power: 1
Lauramc is on a distinguished road

Awards Showcase
Microsoft SQL Server 
Total Awards: 1

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
Sponsored Links
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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Create History Table / Outcome results Rebelle Database Design Help 9 July 1st, 2008 08:33 AM
[ASP.Net/General] Create ToolTip for GridView Header jmurrayhead 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 02:23 PM.



Content Relevant URLs by vBSEO 3.2.0