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!

How can I stop a loop when the right mouse button is selected? 4

Status
Not open for further replies.

JensKKK

Technical User
May 8, 2007
119
GB
How can I interrupt the execution of a do while loop when the right mousebutton was selected.

Any help appreciated.
 
Jens, as far as I am aware, once in a loop the Vb IDE will not respond to mouse clicks, only ctrl-break will stop execution.

Nick
 
[Esc] will also interrupt the code.

[tt]_____
[blue]-John[/blue][/tt]
[tab][red]The plural of anecdote is not data[/red]

Help us help you. Please read FAQ181-2886 before posting.
 
...unless you have application.enablecancelkey = false ;-)

Rgds, Geoff

We could learn a lot from crayons. Some are sharp, some are pretty and some are dull. Some have weird names and all are different colours but they all live in the same box.

Please read FAQ222-2244 before you ask a question
 
Would DoEvents help at all? For example if you put a conditional DoEvents call in the middle of your loop to execute on every nth iteration, would it allow you to catch userform's MouseDown event, and maybe set a flag that could be checked inside the loop, allowing you to bail out early?



Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
JensKKK,
I'm guessing that you have a long running routine based on a loop and you want to provide the ability to 'pause' or cancel the routine to your user?

And yeah you got it. Create a global variable ([tt]blnCancel[/tt]) that you set with the right click event then use [tt]DoEvents[/tt] and an exit statement in your loop:
Code:
...
Do
  'Fire DoEvents every 100th iterations
  If Counter mod 100 = 0 then
    DoEvents
  End If
  'Exit sequence
  If blnCancel Then
    Goto Routine_Exit
  End If
Loop Until [i]Done[/i]
...

With a little extra work you could actually make this 'pause' the execution by storing an entry/exit point in a global variable...

Hope this helps,
CMP

[small]For the best results do what I'm thinking, not what I'm saying.[/small]
(GMT-07:00) Mountain Time (US & Canada)
 
stevexff mentions capturing the MouseDown event on the userform, but....NO userform has been mentioned.

So how are you setting it with a right click event? How do you detect a right click while it is running? I can see setting that global before the loop starts, but how do you set it when it is running?

faq219-2884

Gerry
My paintings and sculpture
 
I only mentioned it because you need to have something visible on the UI to capture a MouseDown event, and a UserForm seemed like a good candidate. Even if you have to make one specially...

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
funei,

I am working on a program that does curve fitting of several 1000 data points.

Once the program starts it can easily run for 45 minutes. Some user might want to stop the execution because they see that thier data are not good or they might want to change some of the curve parameters.

Jens
 
CMP,

thanks for suggestion. Quick question how do I declare

blnCancel?

Thanks again.


JensKKK,
I'm guessing that you have a long running routine based on a loop and you want to provide the ability to 'pause' or cancel the routine to your user?

And yeah you got it. Create a global variable (blnCancel) that you set with the right click event then use DoEvents and an exit statement in your loop:
CODE
...
Do
'Fire DoEvents every 100th iterations
If Counter mod 100 = 0 then
DoEvents
End If
'Exit sequence
If blnCancel Then
Goto Routine_Exit
End If
Loop Until Done
...

With a little extra work you could actually make this 'pause' the execution by storing an entry/exit point in a global variable...

Hope this helps,
CMP
 
how do I declare blnCancel?
In the Declarations section, ie before any procedure:
Dim blnCancel As Boolean

Be sure to set its value to False before entering your loop.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
stevexff said:
Would DoEvents help at all? For example if you put a conditional DoEvents call in the middle of your loop to execute on every nth iteration, would it allow you to catch userform's MouseDown event, and maybe set a flag that could be checked inside the loop, allowing you to bail out early?
Stars all round boys... I'm glad I thought of it [sad]

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
OK guys so far I got this.

But how does this link the doevents with the blnCancel variable. Something is missing here to get this to work. How do I link doevents and blnCancel together?



Dim blnCancel As Boolean

Do
'Fire DoEvents every 100th iterations
If Counter mod 100 = 0 then
DoEvents
End If
'Exit sequence
If blnCancel Then
Goto Routine_Exit
End If
Loop Until Done
...
 
You must have some event procedure somewhere that set the boolean to true at the user request.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Precisely...that is what I was asking. WHAT event is going to do that? If it is not a userform WHAT is capturing the mouse event????

faq219-2884

Gerry
My paintings and sculpture
 
JensKKK,
How is your program started?

Is it fired from a form, from a menu bar, macro, is the user opening up the routine in the VBE and pressing play or are you in the stage where your still developing and haven't yet packaged it for end user consumption?

CMP

[small]For the best results do what I'm thinking, not what I'm saying.[/small]
(GMT-07:00) Mountain Time (US & Canada)
 
CMP,

The program macro can be started from a command button or from the menu.

So far I got several replies to my question, but I still don't get it to work. Unfortunately I have not used the doEvents command and I have therefore no clue on how to link the doevents command to the Boolean variable blnCancel. Could explain that to me please. Or could you point me to a Doevents example.

Many thanks.

 
Maybe I missed something, but what is the host application you work with?

combo
 
Combo,
I am working in MS Excel. If that is waht you after.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top