Solved

Help with eratic loop counter in a timer

Posted on 2014-02-27
13
178 Views
Last Modified: 2014-02-28
This counter keeps starting over for some reason.(miCounter)
i have changed the name of the counter in case it was referenced elsewhere in my code
sometimes it gets to 10 and starts over other times 11
The code;
Form level variable: Dim miCounter As Integer

Private Sub Timer1_Timer()
Dim bFinished As Boolean
Debug.Print "mcurCurWinPlusBonus " & mcurCurWinPlusBonus       
         
          If mintCreditMode = 1 Then 'currency
            lblCredit.Caption = CCur(lblCredit.Caption) + mdblDecimalPart 'WinAmount
            lblCredit.Caption = Format(lblCredit.Caption, "currency")
            lblWinThisRun.Caption = lblWinThisRun.Caption + 1
            miCounter = miCounter + 1
             Debug.Print "miCounter " & miCounter & " lblCredit.Caption " & lblCredit.Caption
          Else
              lblWinThisRun.Caption = lblWinThisRun.Caption + 1 '3rd
              lblCredit.Caption = Val(lblCredit.Caption) + 1
'             miCounter = miCounter + 1
          End If
     
    If miCounter >= mcurCurWinPlusBonus Then 'mlngCreditsWon Then '4
        Debug.Print "end Run"
           
         End If
 Loop
           
            Timer1.Enabled = False           
            UpdatePerformanceGridsWin
End Sub

Open in new window


The debug results:
mcurCurWinPlusBonus 110
miCounter 1 lblCredit.Caption $0.51
miCounter 2 lblCredit.Caption $0.52
miCounter 3 lblCredit.Caption $0.53
miCounter 4 lblCredit.Caption $0.54
miCounter 5 lblCredit.Caption $0.55
miCounter 6 lblCredit.Caption $0.56
miCounter 7 lblCredit.Caption $0.57
miCounter 8 lblCredit.Caption $0.58
miCounter 9 lblCredit.Caption $0.59
miCounter 10 lblCredit.Caption $0.60
miCounter 1 lblCredit.Caption $0.61
miCounter 2 lblCredit.Caption $0.62
miCounter 3 lblCredit.Caption $0.63
miCounter 4 lblCredit.Caption $0.64
miCounter 5 lblCredit.Caption $0.65
miCounter 6 lblCredit.Caption $0.66
miCounter 7 lblCredit.Caption $0.67
miCounter 8 lblCredit.Caption $0.68
miCounter 9 lblCredit.Caption $0.69
miCounter 10 lblCredit.Caption $0.70
miCounter 11 lblCredit.Caption $0.71
miCounter 1 lblCredit.Caption $0.72
miCounter 2 lblCredit.Caption $0.73
miCounter 3 lblCredit.Caption $0.74
miCounter 4 lblCredit.Caption $0.75
miCounter 5 lblCredit.Caption $0.76
miCounter 6 lblCredit.Caption $0.77
miCounter 7 lblCredit.Caption $0.78
miCounter 8 lblCredit.Caption $0.79
miCounter 9 lblCredit.Caption $0.80
miCounter 10 lblCredit.Caption $0.81
miCounter 11 lblCredit.Caption $0.82
miCounter 1 lblCredit.Caption $0.83
miCounter 2 lblCredit.Caption $0.84
miCounter 3 lblCredit.Caption $0.85
miCounter 4 lblCredit.Caption $0.86
miCounter 5 lblCredit.Caption $0.87
miCounter 6 lblCredit.Caption $0.88
miCounter 7 lblCredit.Caption $0.89
miCounter 8 lblCredit.Caption $0.90
miCounter 9 lblCredit.Caption $0.91
miCounter 10 lblCredit.Caption $0.92
miCounter 11 lblCredit.Caption $0.93
miCounter 12 lblCredit.Caption $0.94
miCounter 13 lblCredit.Caption $0.95
miCounter 14 lblCredit.Caption $0.96
miCounter 15 lblCredit.Caption $0.97
miCounter 16 lblCredit.Caption $0.98
miCounter 17 lblCredit.Caption $0.99
miCounter 18 lblCredit.Caption $1.00
miCounter 19 lblCredit.Caption $1.01
miCounter 20 lblCredit.Caption $1.02
miCounter 21 lblCredit.Caption $1.03
miCounter 22 lblCredit.Caption $1.04
miCounter 23 lblCredit.Caption $1.05
miCounter 24 lblCredit.Caption $1.06
miCounter 25 lblCredit.Caption $1.07
miCounter 26 lblCredit.Caption $1.08
miCounter 27 lblCredit.Caption $1.09
miCounter 28 lblCredit.Caption $1.10
miCounter 29 lblCredit.Caption $1.11
miCounter 30 lblCredit.Caption $1.12
miCounter 31 lblCredit.Caption $1.13
miCounter 32 lblCredit.Caption $1.14
miCounter 33 lblCredit.Caption $1.15
miCounter 34 lblCredit.Caption $1.16
miCounter 35 lblCredit.Caption $1.17
miCounter 36 lblCredit.Caption $1.18
miCounter 37 lblCredit.Caption $1.19
miCounter 38 lblCredit.Caption $1.20
miCounter 39 lblCredit.Caption $1.21
miCounter 40 lblCredit.Caption $1.22
miCounter 41 lblCredit.Caption $1.23
miCounter 42 lblCredit.Caption $1.24
miCounter 43 lblCredit.Caption $1.25
miCounter 44 lblCredit.Caption $1.26
miCounter 45 lblCredit.Caption $1.27
miCounter 46 lblCredit.Caption $1.28
miCounter 47 lblCredit.Caption $1.29
miCounter 48 lblCredit.Caption $1.30
miCounter 49 lblCredit.Caption $1.31
miCounter 50 lblCredit.Caption $1.32
miCounter 51 lblCredit.Caption $1.33
miCounter 52 lblCredit.Caption $1.34
miCounter 53 lblCredit.Caption $1.35
miCounter 54 lblCredit.Caption $1.36
miCounter 55 lblCredit.Caption $1.37
miCounter 56 lblCredit.Caption $1.38
miCounter 57 lblCredit.Caption $1.39
miCounter 58 lblCredit.Caption $1.40
miCounter 59 lblCredit.Caption $1.41
miCounter 60 lblCredit.Caption $1.42
miCounter 61 lblCredit.Caption $1.43
miCounter 62 lblCredit.Caption $1.44
miCounter 63 lblCredit.Caption $1.45
miCounter 64 lblCredit.Caption $1.46
miCounter 65 lblCredit.Caption $1.47
miCounter 66 lblCredit.Caption $1.48
miCounter 67 lblCredit.Caption $1.49
miCounter 68 lblCredit.Caption $1.50
miCounter 69 lblCredit.Caption $1.51
miCounter 70 lblCredit.Caption $1.52
miCounter 71 lblCredit.Caption $1.53
miCounter 72 lblCredit.Caption $1.54
miCounter 73 lblCredit.Caption $1.55
miCounter 74 lblCredit.Caption $1.56
miCounter 75 lblCredit.Caption $1.57
miCounter 76 lblCredit.Caption $1.58
miCounter 77 lblCredit.Caption $1.59
miCounter 78 lblCredit.Caption $1.60
miCounter 79 lblCredit.Caption $1.61
miCounter 80 lblCredit.Caption $1.62
miCounter 81 lblCredit.Caption $1.63
miCounter 82 lblCredit.Caption $1.64
miCounter 83 lblCredit.Caption $1.65
miCounter 84 lblCredit.Caption $1.66
miCounter 85 lblCredit.Caption $1.67
miCounter 86 lblCredit.Caption $1.68
miCounter 87 lblCredit.Caption $1.69
miCounter 88 lblCredit.Caption $1.70
miCounter 89 lblCredit.Caption $1.71
miCounter 90 lblCredit.Caption $1.72
miCounter 91 lblCredit.Caption $1.73
miCounter 92 lblCredit.Caption $1.74
miCounter 93 lblCredit.Caption $1.75
miCounter 94 lblCredit.Caption $1.76
miCounter 95 lblCredit.Caption $1.77
miCounter 96 lblCredit.Caption $1.78
miCounter 97 lblCredit.Caption $1.79
miCounter 98 lblCredit.Caption $1.80
miCounter 99 lblCredit.Caption $1.81
miCounter 100 lblCredit.Caption $1.82
miCounter 101 lblCredit.Caption $1.83
miCounter 102 lblCredit.Caption $1.84
miCounter 103 lblCredit.Caption $1.85
miCounter 104 lblCredit.Caption $1.86
miCounter 105 lblCredit.Caption $1.87
miCounter 106 lblCredit.Caption $1.88
miCounter 107 lblCredit.Caption $1.89
miCounter 108 lblCredit.Caption $1.90
miCounter 109 lblCredit.Caption $1.91
miCounter 110 lblCredit.Caption $1.92
End Run
Why is this happing ?
0
Comment
Question by:isnoend2001
  • 7
  • 4
  • 2
13 Comments
 
LVL 45

Expert Comment

by:Martin Liss
Comment Utility
Go into Debug and add a watch that looks like this.

Watch
The second time it finds that the value is 1 and stops, look at the Call stack to see where it happened.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
What is line #23, "Loop", paired with?...
0
 

Author Comment

by:isnoend2001
Comment Utility
I put the loop incase user does not want to wait for labels to rotate
clicking the form sets bFinished to true
Do While bFinished = False
0
 

Author Comment

by:isnoend2001
Comment Utility
thanks MartinLiss that breaks whebn its 1, but i cannot determine why
0
 

Author Comment

by:isnoend2001
Comment Utility
I have never used the timer in any projects, but i have had nothing but problems with it.
The last being when the labels were rotating the code would not recognize form clicks.
finally adding a DoEvents overcame that problem. Don't remember the other problems.
It just seems that when a timer has control results can be unpredictable
i am looking at using Sleep instead of a timer to control label rotation speeds and maybe have more control of the code
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
You shouldn't have a loop and a timer...especially a loop inside the timer.  Get rid of the loop.  When the user wants to cancel all you need to do is turn off the timer.
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 45

Expert Comment

by:Martin Liss
Comment Utility
Attach your current code, tell me how to reproduce the problem, and I'll take a look at it.
0
 

Author Comment

by:isnoend2001
Comment Utility
Martin Liss thanks
i am preparing the project for upload
0
 

Author Comment

by:isnoend2001
Comment Utility
I uploaded the file to here:
http://roofgenius.com/for%20EE.zip
0
 
LVL 45

Accepted Solution

by:
Martin Liss earned 500 total points
Comment Utility
micounter is set to 0 in the AddRunWinning sub and that sub is called from the Generate20Numbers sub, so any time the Generate20Numbers is run there is the potential for micounter to get reset to 0.

I say "potential" because when I  look at the code in Generate20Numbers it's easy to see that if the If mbWinningRun = True And mbLastBallHitMarkedNumber = False Then line resolves to True then AddRunWinning is called (and so micounter gets set to 0), but the lack of proper indenting in Generate20Numbers causes it to be impossible to tell under what conditions the If mbWinningRun = True And mbLastBallHitMarkedNumber = False Then line is executed.

So I suggest you take the time to reformat that sub and then if it's not clear why that line is executed unintentionally then add Watches for mbWinningRun and mbLastBallHitMarkedNumber that will pause the code when those values change and you'll probably be able to fix the bug.

If you still can't find the bug after that then post the reformatted sub and I'll take another look.
0
 

Author Closing Comment

by:isnoend2001
Comment Utility
Thanks, that fixes the issue. the timer was being called while running.
Easy fix
0
 
LVL 45

Expert Comment

by:Martin Liss
Comment Utility
You're welcome. I don't know if you reformatted that sub or not but you should. In the long run it will pay off.
0
 

Author Comment

by:isnoend2001
Comment Utility
Thanks I thought there was a problem with the timer, been working on replacing it using sleep to control label rotation. Still need to reformat the code. Glad you are here
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now