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.
13 17897
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.
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 :)
lol, same idea, posted the same time :)
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)
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.
= What is a flag and how can I use it?
right at the top of your code out of any routiene put this:
in your module code put this - if userexit = true then exit sub
then on the command button put this
That will do it :)
right at the top of your code out of any routiene put this:
in your module code put this - if userexit = true then exit sub
then on the command button put this
That will do it :)
Cheers, will try that now!
-
Do While x < 50
-
For x = 0 To 7
-
DoEvents
-
Next x
-
For x = 6 To 0 Step -1
-
Do Events
-
Next x
-
Loop
-
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?
right at the top of your code out of any routiene put this:
in your module code put this - if userexit = true then exit sub
then on the command button put this
That will do it :)
Hello again, I implemented your code and the program still crashes. Also, when you said to place the
at the top of the code outside of all routines, I presumed you meant that I was to put it above the - 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?
-
Do While x < 50
-
For x = 0 To 7
-
DoEvents
-
Next x
-
For x = 6 To 0 Step -1
-
Do Events
-
Next x
-
Loop
-
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!
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
at the top of the code outside of all routines, I presumed you meant that I was to put it above the - 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?
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.
This post was very helpful to me. Having to do on the job training.
Thank you!
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Jordan Rastrick |
last post by:
First, a disclaimer. I am a second year Maths and Computer Science
undergraduate, and this is my first time ever on Usenet (I guess I'm
part of the http generation). On top of that, I have been...
|
by: Lauren Wilson |
last post by:
Hello group,
Somehow, I have not yet fully understood the meaning and purpose of
"DoEvents". Can someone post or point me to a clear, concise
explanation of it -- one that includes situational...
|
by: Jim P. |
last post by:
I'm having trouble returning an object from an AsyncCallback called inside a
threaded infinite loop.
I'm working on a Peer2Peer app that uses an AsyncCallback to rerieve the
data from the remote...
|
by: John Pass |
last post by:
What is the difference between a While and Do While/Loop repetition structure.
If they is no difference (as it seems) why do both exist?
|
by: haijin.biz |
last post by:
why the following program using "char" gives me an infinite loop? If I
change char to int, it works fine.
#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
for(char...
|
by: archeryguru2000 |
last post by:
Hello, I've been stumped for several weeks on this particlar program. At work here, we have 30 machines that we record scrap data on. I take this data and create spreadsheets (boring) that can be...
|
by: James Watt |
last post by:
can anyone tell me how to do an infinite loop in C/C++, please ?
this is not a homework question .
|
by: Pivot_Tables |
last post by:
Hi,
I have created a recursive SQL Query in DB2 and it works fine until
some point in the tree where the data gets into infinite loop. Below
are some sample data from my relationship table.
...
|
by: robotlizz |
last post by:
Hello - I am a brand new at Java and I am having a hard time with a program I have to turn in tomorrow. I can not get the 'Q' option to work and the loop goes on forever. I've tried to go over the...
|
by: MeoLessi9 |
last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: marcoviolo |
last post by:
Dear all,
I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
| |