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

Breaking a "Do While" Infinite Loop using a command button

P: 5
Hello, I'm a 2nd year Computer Engineering student and I have a problem with my VB6 code. I've just started learning VB6 for a project in which we have to create a form which displays the knight-rider light sequence. Obviously, this means using a loop and our lecturer has told us to use timers to control how long the sequence lasts for. I don't want to use timers ((this is not due to laziness, I would have been finished long ago had I used timers!! I just want to see if there is another way to do this project) , and I've read everywhere that the only way to break out of an infinite loop is Ctl+Alt+Del! What I am asking is - is there any code I could use to over-write the loop which I could also link with a command button (i.e. when I hit my 'exit' button on my form, the program stops runnning). If this is not possible, a way in which the user could input a number which would stop the program would also be a great help. The module I'm studying this for is interfacing, so user-compatability is paramount. Perhaps there is a different loop I could use which offers more flexibility?

Here is an outline of my code so far:

Private Sub cmdstart_click()
Dim Index As Integer

Do While x < 50 (There's no particular reason why I chose 50, I just wanted to create an infinite loop)

For x = 0 To 7

DoEvents

Next x

For x = 6 To 0 Step -1

Do Events

Next x

Loop




End Sub

Code for 'Exit' Button

Private Sub Command1_Click()
Unload Me
End Sub

I haven't posted the 'events' in the code, as the posting guidlines advise that you shouldn't post all of your code. I need to have this done in two days, so any help is appreciated! Thanks in advance, Anna.
Oct 3 '07 #1
Share this Question
Share on Google+
13 Replies


hariharanmca
100+
P: 1,977
Your code doesn’t make any sense. Because X will not go beyond 50.
use boolen Flag, timer control if the flag is false then run loop if it true then stop.
Oct 3 '07 #2

P: 24
Surely you'd just set a flag?

at the top of your project set userexit as boolian

then in your loop check if userexit = true is so then exit the loop

then on the command button (user input) just set userexit = true.


in 'theory' that would work, try it :)
Oct 3 '07 #3

P: 24
lol, same idea, posted the same time :)
Oct 3 '07 #4

hariharanmca
100+
P: 1,977
lol, same idea, posted the same time :)
Cheer up code937.

(The message you have entered is too short. Please lengthen your message to at least 20 characters)
Oct 3 '07 #5

P: 5
Your code doesn’t make any sense. Because X will not go beyond 50.
use boolen Flag, timer control if the flag is false then run loop if it true then stop.
That was the idea, that it never goes beyond x, simply because I wanted to loop to run until the user decides to exit. However, exiting a loop is not so easy! What is a flag and how can I use it? I only started learning VB6 a week or two ago, so I'm not familiar with every nook and cranny, as it were! Thanks for your reply.
Oct 3 '07 #6

P: 24
= What is a flag and how can I use it?
right at the top of your code out of any routiene put this:

Expand|Select|Wrap|Line Numbers
  1. dim userxit as Boolean
in your module code put this

Expand|Select|Wrap|Line Numbers
  1. if userexit = true then exit sub
then on the command button put this

Expand|Select|Wrap|Line Numbers
  1. userexit = true
That will do it :)
Oct 3 '07 #7

P: 5
right at the top of your code out of any routiene put this:

Expand|Select|Wrap|Line Numbers
  1. dim userxit as Boolean
in your module code put this

Expand|Select|Wrap|Line Numbers
  1. if userexit = true then exit sub
then on the command button put this

Expand|Select|Wrap|Line Numbers
  1. userexit = true
That will do it :)
Cheers, will try that now!
Oct 3 '07 #8

hariharanmca
100+
P: 1,977
Expand|Select|Wrap|Line Numbers
  1. Do While x < 50 
  2. For x = 0 To 7
  3. DoEvents
  4. Next x
  5. For x = 6 To 0 Step -1
  6. Do Events
  7. Next x
  8. Loop
  9. End Sub
First 'for' will not do any thing (only Do events)
and also Same to second 'for'.
for this why don't you use a timer control?
Oct 3 '07 #9

P: 5
right at the top of your code out of any routiene put this:

Expand|Select|Wrap|Line Numbers
  1. dim userxit as Boolean
in your module code put this

Expand|Select|Wrap|Line Numbers
  1. if userexit = true then exit sub
then on the command button put this

Expand|Select|Wrap|Line Numbers
  1. userexit = true
That will do it :)
Hello again, I implemented your code and the program still crashes. Also, when you said to place the
Expand|Select|Wrap|Line Numbers
  1. dim userexit as Boolean
at the top of the code outside of all routines, I presumed you meant that I was to put it above the
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdStart_Click()
line as opposed to underneath it. However, when I try to compile this, I get a message saying "Compile Error: Member already exists in an object module from which this object module derives". Am I placing the code you've given me in the wrong place?
Oct 4 '07 #10

P: 5
Expand|Select|Wrap|Line Numbers
  1. Do While x < 50 
  2. For x = 0 To 7
  3. DoEvents
  4. Next x
  5. For x = 6 To 0 Step -1
  6. Do Events
  7. Next x
  8. Loop
  9. End Sub
First 'for' will not do any thing (only Do events)
and also Same to second 'for'.
for this why don't you use a timer control?
I realise that 'for' can't do events, the 'DoEvents' was a phrase I used merely to represent a block of code, as the posting guidelines state that you shouldn't post all of your code. Sorry about that, it must have looked very strange!! Anyway, my lecturer did tell me to use timer controls, but I'm interested in finding out if you can break an infinite loop because that would be a much better way of doing things, if at all possible. Here's hoping!
Oct 4 '07 #11

P: 24
this refers toa routiene you have called Member, find it change the name and change where you call it from.

The Dimming is correct :)


Hello again, I implemented your code and the program still crashes. Also, when you said to place the
Expand|Select|Wrap|Line Numbers
  1. dim userexit as Boolean
at the top of the code outside of all routines, I presumed you meant that I was to put it above the
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdStart_Click()
line as opposed to underneath it. However, when I try to compile this, I get a message saying "Compile Error: Member already exists in an object module from which this object module derives". Am I placing the code you've given me in the wrong place?
Oct 6 '07 #12

Expert 5K+
P: 8,434
this refers toa routiene you have called Member, find it change the name and change where you call it from.

The Dimming is correct :)
code937, I think you're sending the OP on a bum steer, here. It's highly unlikely that he/she has a routine called Member. I think he/she didn't understand where to place form-level declarations.

Sunbags, could you show us the code that you have? That "Member already exists" thing should be pretty simple to correct.

And to try and clarify things a bit...
  • You do need to have a DoEvents statement somewhere inside your infinite loop. This statement tells your program to take a break, and allow Windows to handle other important things, such as the user interface. Without that, you will not be able to click the button to set the flag, because Windows won't be able to respond to the click.
  • The problems with the "userexit" declaration relate to variable scope. You may or may not have covered this. If you declare (Dim) a variable inside a routine (Sub or Function) then it only exists within that routine. Which means that if you declare it in the routine that has the loop, nothing else outside oif that routine (such as the button click event) can touch it. Which makes it, of course, completely useless.
    What you need to do is put the Dim (or Private) statement at the top of the form code, before any of the Sub or Function declarations. In other words, at the top of the form's code you would have something like...
    Option Explicit
    Private userexit As Boolean
    .
    .
    .
    ' Then your Subs and Functions
  • If you just moved the Dim statement "above" the Sub, it was probably still after some other code. Which is probably what caused the compile error.
  • In case you're wondering, the Option Explicit is not actually required, but does prevent a lot of problems. In your options, you should have "Require explicit variable declaration" turned on. What that does is place this statement at the top of each module by default. This statement tells VB to insist that you create each variable before using it. Without this, any time you use a variable name that hasn't been declared, VB will just create one of the default type (probably Variant). Sounds nice, but it means any time you type a variable name wrong, VB won't tell you. It'll just create a new variable and keep going, and you can waste days trying to work out where your information went.
Oct 6 '07 #13

P: 1
This post was very helpful to me. Having to do on the job training.
Thank you!
Sep 17 '08 #14

Post your reply

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