Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trying to make a simple counter using a timer

Status
Not open for further replies.

jav0

MIS
Nov 1, 2001
8
US
Hello,
I'm trying to make a simple (Should be) counter using a timer that will change the caption on a label every second.
This is what Ive tried.
In the Form I have this
Counter = 0

In the timer I have this
Counter = Counter + 1
Label1.Caption = Counter

The interval of the timer is set to 1000.
I've got to be close.. Any ideas??

Thanks for the help,
 
You will need to refresh the form:

Me.Refresh

 
Didnt work.. This is what I have.
_______________________________________
Private Sub Form_Load()
Counter = 0
Form1.Refresh
End Sub
____________________________________
Private Sub Timer1_Timer()
Counter = Counter + 1
Label1.Caption = Counter

End Sub
______________________________________

I also tried the refresh under label1.caption = counter in the properties of the timer.

Thanks again for the help,
 

Personally, I'd go with the DoEvents instead of the Refresh but both are supposed to produce the same visuals. THis means that we are not fully appreciating the nature of your problem. So instead of asking if the Label is visible, I'll go with "What is the specific problem?"


Wil Mead
wmead@optonline.net

 
What it's doing is, the caption on the label will change to 1, then nothing..

What I have in the above message is the entire program. Im learning and piecing stuff togeather as it comes along.

Thanks again for your help,
 
That was the entire program? Well that explains it. As it appears above, Counter is an undeclared, uninitialized local variable. In other words, every time you enter the Time event procedure, Counter is recreated and reinitialized to the default of 0. To fix this, add the following 2 lines to the very top of your program:

Option Explicit
Private Counter As Long

The first line will warn you of any undeclared variables. The second will ensure that every procedure in the form module can see Counter.
 
You might also try:

Label1.caption = Val(label1.caption) + 1

You may have to do the string conversion explicitly.

Wil Mead
wmead@optonline.net

 
When I add those 2 lines to the top of the program I get
Compile Error
Invalid Inside Procedure

Private Sub Form_Load()
Option Explicit
Private Counter as Long
Counter = 0
Form1.Refresh
End Sub
____________________________________
Private Sub Timer1_Timer()
Counter = Counter + 1
Label1.Caption = Counter

End Sub
______________________________________



Should I take anything out or add anything?

Thanks,


 
He meant put those 2 lines at the VERY top of the program (in the General Declaraions section) not in the Form_Load event.
 
WilMead Thank You!!!!!!!!!That did it..


Thanks to all others for your efforts as well

 
jav0,
I know you already got it working, and I apologize if you already know this stuff, but since you sound like a beginner, I thought the following might be useful to you.

For the record, brailian is right about my intentions with those 2 lines. Option statements should always go in the general declarations section. The Option Explicit statement forces you to explicitly declare every variable you use with a Dim, Private, or Public statement. You should always have it as the first line of every module you use, as it enables you to catch lots of typos and other errors. For example, if you had had the Option Explicit beginning your program but not the declaration for Counter, the compiler would have raised a "variable not declared" error when you tried to run the program, rather than just letting it do something you didn't want.

As for the Private statement, let me run down the scope rules real quick.
1) Module level variables: These are declared with Dim or Private in the General Declarations section. Every procedure in the module can see them and they last for the life of the module. (For .bas modules, this means as long as the program runs, for form modules, this means as long as the form is loaded.)
2) Local variables: They are declared with Dim or Private in a Sub or Function. If you use implicit declaration (as you did), then the variables in your Subs will be local by default. Then last only until the Sub ends. For event procedures, this means that the variable will be created anew every time the events fires, with none of the information being retained between calls.
The upshot of this is that your code was completely correct, except that the Counter variables in the Form_Load and Time event procedures were both local, and thus completely different variables. Remember that two variables can have the same name if their scopes are different, i.e. if they can't both exist at the same time. If you used the Private Counter As Integer statement in the general declarations section, then the value of Counter would have survived between calles to the Time event, and it would have worked perfectly.
 
Adahacker,

Thanks a bunch for the info. Yes Im a beginner, and I apologize if I insulted you in any way. Most of this stuff is over my head and I'm trying my best to figure it out.
Thanks for breaking it down for me in your last message that really helps me to learn.

Thanks again,
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top