Counting number of clicks

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bupanda
    New Member
    • Apr 2007
    • 7

    Counting number of clicks

    I want to write a code for one of my command buttons (cmdCalculate) and that it shows the number of times it has been clicked in the lblGroupCount.

    I went through some books and I found this two codes that are useful but I still dont know how it works.
    This is what i have so far


    Private Sub cmdCalculate_Cl ick()
    'Calculate the total number of groups, average and total charges.

    Dim GroupCount As Integer

    then I dont know where to put GroupCount = GroupCount +1...

    At the end I have
    lblGroupCount.C aption = FormatNumber(Go rupCount)

    can anyone tell me what I am doing wrong?
  • ansumansahu
    New Member
    • Mar 2007
    • 149

    #2
    Originally posted by bupanda
    I want to write a code for one of my command buttons (cmdCalculate) and that it shows the number of times it has been clicked in the lblGroupCount.

    I went through some books and I found this two codes that are useful but I still dont know how it works.
    This is what i have so far


    Private Sub cmdCalculate_Cl ick()
    'Calculate the total number of groups, average and total charges.

    Dim GroupCount As Integer

    then I dont know where to put GroupCount = GroupCount +1...

    At the end I have
    lblGroupCount.C aption = FormatNumber(Go rupCount)

    can anyone tell me what I am doing wrong?
    Declare the GroupCount as a global variable. Then inside the button_Click funtion increment the counter by one.

    Hope this helps.
    -ansuman sahu

    Comment

    • devonknows
      New Member
      • Nov 2006
      • 137

      #3
      Originally posted by ansumansahu
      Declare the GroupCount as a global variable. Then inside the button_Click funtion increment the counter by one.

      Hope this helps.
      -ansuman sahu
      A little more insight to that just to help ya a little, If you want the count to go over multiple forms.. i.e. Form1, Form2, Form3 then its advised that you declare it as.. Public GroupCount as Long inside a module so that all forms can access it, or if it is just for 1 form you can do this for example...

      Code:
      Option Explicit
      Private GroupCount as Long      'Private Variable
      
      Private Sub cmdCalculate_Click()
      
          'Increment The GroupCount Var by 1
          GroupCount = GroupCount + 1
      
          'Update The Label
          lblGroupCount = GroupCount
      
      End Sub
      I Hope this helps you, Kind Regards
      Devon.

      Comment

      • Killer42
        Recognized Expert Expert
        • Oct 2006
        • 8429

        #4
        And to clarify (or confuse) even more...

        If you want the count to be available only inside the button's click routine, then define it there as Static. Unlike normal local variables, a static variable is not discarded and recreated each time you exit and come back to a routine.

        To summarise, the options we have so far are...
        • Global
          Defined in a code module (not form) as Public.
          Available everywhere in your project.
        • Form-level
          Defined in the declarations section at the top of your form, as Private.
          Available to all code and controls in that form.
        • Static local
          Defined within the routine where it is to be used (click event, in this case) as Static (instead of Private or Public).
          Available only within that routine, but doesn't get erased and re-created each time you leave the routine, like a Private variable.

        Comment

        Working...