I'm new to VB...Really new. The main thing I am needing to accomplish is this program I am trying to make. It's just a counter. You click the button and it shows the number of clicks in the label. Well, There are 14 counters in the one program. But I can't get them to go past 1...I was hoping someone could take a look at it for me... and I hope I don't mess up the [] things lol Oh yea, it'd be used for counting cards lol

Private Sub Aces_Click()
Dim CounterVariable As Integer
CounterVariable = CounterVariable + 1
Label1 = CounterVariable
End Sub
Private Sub Eights_Click()
Dim CounterVariable8 As Integer
CounterVariable8 = CounterVariable8 + 1
Label8 = CounterVariable8
End Sub
Private Sub Fives_Click()
Dim CounterVariable5 As Integer
CounterVariable5 = CounterVariable5 + 1
Label5 = CounterVariable5
End Sub
Private Sub Fours_Click()
Dim CounterVariable4 As Integer
CounterVariable4 = CounterVariable4 + 1
Label4 = CounterVariable4
End Sub
Private Sub Jacks_Click()
Dim CounterVariable11 As Integer
CounterVariable11 = CounterVariable11 + 1
Label11 = CounterVariable11
End Sub
Private Sub Jokers_Click()
Dim CounterVariable14 As Integer
CounterVariable14 = CounterVariable14 + 1
Label14 = CounterVariable14
End Sub
Private Sub Kings_Click()
Dim CounterVariable13 As Integer
CounterVariable13 = CounterVariable13 + 1
Label13 = CounterVariable13
End Sub
Private Sub Nines_Click()
Dim CounterVariable9 As Integer
CounterVariable9 = CounterVariable9 + 1
Label9 = CounterVariable9
End Sub
Private Sub Queens_Click()
Dim CounterVariable12 As Integer
CounterVariable12 = CounterVariable12 + 1
Label12 = CounterVariable12
End Sub
Private Sub Sevens_Click()
Dim CounterVariable7 As Integer
CounterVariable7 = CounterVariable7 + 1
Label7 = CounterVariable7
End Sub
Private Sub Sixs_Click()
Dim CounterVariable6 As Integer
CounterVariable6 = CounterVariable6 + 1
Label6 = CounterVariable6
End Sub
Private Sub Tens_Click()
Dim CounterVariable10 As Integer
CounterVariable10 = CounterVariable10 + 1
Label10 = CounterVariable10
End Sub
Private Sub Threes_Click()
Dim CounterVariable3 As Integer
CounterVariable3 = CounterVariable3 + 1
Label3 = CounterVariable3
End Sub
Private Sub Twos_Click()
Dim CounterVariable2 As Integer
CounterVariable2 = CounterVariable2 + 1
Label2 = CounterVariable2
End Sub

Thank you all in advance!

Recommended Answers

All 8 Replies

Declare your variables at the form level, if you declare in the procedural level, they get reset each time the sub or function runs.

You can declare them at the form level, or you could simply replace each occurance of Dim with Static. Static allows you to make a procedural variable, that will maintain it's value until manually reset (or the object in which it resides is destroyed). It's good programming etiquette to use static as opposed to declaring your variables at the form's scope, because in this case, you have no need or desire to use that same variable in a different procedure on the same form. This keeps structure and organization in your code, and doesn't use as much memory by having to span the variable to different procedures.

Thank You both so much. I wish I could say I understood everything, but that'd be a lie. lol I did understand the part where I just had to switch DIM for Static, and it's working perfect. I think I am going to attempt a reset button. Which means you'll probably be hearing from me again, but I will give it a shot first. Once again thank you!


it's easy. for example:
Dim CounterVariable14 As Integer
CounterVariable14 = CounterVariable14 + 1
Label14 = CounterVariable14
End Sub

when you press the button you want to add 1 to your variable(your counter). in the button property i see that you declare the varible: Dim CounterVariable14 As Integer. understood till here? but when ou declare it, it will have 0 value. so each time you will press the button your variblae will be: 0=0+1. each time the value of the varible in each button will be 1

so what you need is to declare the variable as public. you will have somehting like this at the begineing of the 'code window'

declare type
dim CounterVariable14 As Integer

Private Sub Jokers_Click()
CounterVariable14 = CounterVariable14 + 1
Label14 = CounterVariable14
End Sub

Right, but as per my previous post, this method is depreciated..... it's bad programming practice to put variables outside of their scope of intended use.....

well... ne or not depreciated
i work in VB for about 7 years! i worked in many company in VB and i used variables declared like this! and nothing gone wrong!

I've been working in VB for over a decade (10 years), Interfaced VB with RADIUS Servers, and Linux Side Perl with Sockets, and have been a senior programmer for a corporation doing VB..... I'm not here challenging your credentials (nor should you challenge mine) and while there may not be any errors in doing this kind of programming, it's poorly written code. Anybody who has written code in a language other than VB, understands very well that scope is a very important aspect to programming. Scope keeps variables maintained, and it saves space in RAM by not having to span multiple procedures. Fact is, if you don't need the variable outside of the procedure, you shouldn't declare it outside of the procedure..... it's bad programming practice.

While the solution you provided can/will work, professional code simply should not be written that way. My AIM here is to provide more than a solution..... it's to provide people with a solution that fits into proper programming style.

Now, I'm sorry if I offended you with my previous post negating your post, I understand and appreciate that you are being helpful, but it's important for programmers to understand that vital role of variables in scope.... which Is why I posted what I did.

Hi Comasote,

Sorry for this delay but i have and i am buisy right here.
If you afended me? Oh no.. stay easy.. i never thought about that! Just cose i have not tu much time, now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.