Reputation: 33
I made this loop for one of my classes but for some reason the loop doesn't stop. Here is the code I have
Dim strScore As String
Dim intNumTests As Integer 'counter
Dim sngSumTests As Single 'accumulator
Dim sngAverage As Single
strScore = InputBox("Enter the score. Click cancel when finished.", "Score Entry")
Do While strScore <> " "
intNumTests = intNumTests + 1 ' update Counter
sngSumTests = sngSumTests + Val(strScore)
strScore = InputBox("Enter the score. Click cancel when finished.", "Score Entry")
Loop
Upvotes: 0
Views: 100
Reputation: 5836
For the loop to stop you must type in the InputBox a space, not the actual word space
but press the spacebar once.
Instead of
Do While strScore <> " "
What you really wanted is
Do While strScore <> "" AndAlso strScore <> " "
or
Do While strScore.Length <> 0 AndAlso strScore <> " "
Now the Cancel
button will work properly, and it will keep going if you don't type anything and press OK
button.
If you press the OK
button in InputBox and the input is empty it will return " "
(space).
If you press the Cancel
button in InputBox it will return ""
(empty string)
Edit:
Turns out pressing OK
with a empty InputBox in the .NET Framework 4 also returns (empty string) so it's impossible? to detect the Cancel
button.
Upvotes: 2