By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,852 Members | 2,152 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,852 IT Pros & Developers. It's quick & easy.

Counting number of clicks

P: 7
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_Click()
'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.Caption = FormatNumber(GorupCount)

can anyone tell me what I am doing wrong?
May 4 '07 #1
Share this Question
Share on Google+
3 Replies


100+
P: 149
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_Click()
'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.Caption = FormatNumber(GorupCount)

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
May 4 '07 #2

devonknows
100+
P: 137
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...

Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2. Private GroupCount as Long      'Private Variable
  3.  
  4. Private Sub cmdCalculate_Click()
  5.  
  6.     'Increment The GroupCount Var by 1
  7.     GroupCount = GroupCount + 1
  8.  
  9.     'Update The Label
  10.     lblGroupCount = GroupCount
  11.  
  12. End Sub
  13.  
I Hope this helps you, Kind Regards
Devon.
May 4 '07 #3

Expert 5K+
P: 8,434
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.
May 5 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.