The last time I played with this type of thing, I finished up having to
"break-out" of the executing function. I did this in rather elegant style
by throwing a message box every 20 loops asking if I wanted to continue or
not. I'm just wondering if a refinement on that is to breakout to a
function that tests the public variable.
if TestLoop = 20 then
If BreakOut() Then Exit Do
...
Function Breakout() as boolean
BreakOut = PublicStop
End Function
Then from your form ....
Private Sub cmdStop_Click
PublicStop = True
Endsub
The only thing I'm not sure of is whether the button on the form can alter
the public while your LongLoops is executing. If not, well, elegance is
worth pursuing!
--
Regards,
Kevin
"deko" <de**@nospam.co m> wrote in message
news:Q8******** *************** *************** ***@comcast.com ...
A quick-and-dirty solution would set a global variable to signal to
LongLoops to stop.
sounds interesting.
If the code in bas1 is ONLY called from frm1, it
should be moved to the form, and a module-level global could be used.
It is, but there's so much code it needs to be segregated by purpose.
A more elegant solution would perhaps test a public property in frm1
to check if the Stop button was clicked.
Don't forget to put a DoEvents in the LongLoops code, so the cmdStop
click would happen while it is executing. If you were using a "tight"
loop, it would first execute and THEN the click would occur - a little
too late.
10-4
I think raising an error is the wrong thing to do. It's abusing an
error handler feature for something it was not designed for.
perhaps...
Here's what I've got working:
[code in form frm1]
Private Sub cmdStart_Click
Call bas1.LongLoops( False, parameter2, parameterN)
End Sub
Private Sub cmdStop_Click
Call bas1.LongLoops( True)
End Sub
[code in standard module bas1]
Public Function LongLoops(blnSt op As Boolean, Optional parameter2, Optional
parameterN)
Static blnExit As Boolean
blnExit = blnStop
Do While i < 10000
If blnExit Then Exit Do
DoEvents
[code omitted]
Loop
Do While i < 20000
If blnExit Then Exit Do
DoEvents
[code omitted]
Loop
[code omited]
End Function
The problem (what I thought might be avoided by raising an error) is that I
have to instantiate a bunch of objects to get into the loop (on the second
call made from cmdStop).
This does not work:
[code in standard module bas1]
Public Function LongLoops(blnSt op As Boolean, Optional parameter2, Optional
parameterN)
Static blnExit As Boolean
blnExit = blnStop
If Not blnStop Then
Set obj1 = Object1
Set obj2 = Object2
Set objN = Object N
Do While obj1 < obj2
If blnExit Then Exit Do
DoEvents
Test objN
[code omitted]
Loop
End If
End Function
The reason this does not work is because (as this new example indicates) the
actual code uses objects rather than integers in the looping construct.
So the objects have to be set to get into the loop. So the second call
needs to get into the loop or blnExit does not change - which I find strange
because I thought I didn't need to get into the loop since blnExit is
Static. That is, I thought just getting into the function with the second
call and changing the value of the Static variable (without getting into the
loop) would cause the Static variable to change regardless of where it is
being used.
Perhaps this would be the case with a global variable? That would be better
because I wouldn't have to instantiate all those objects (just to get into
the loop so I can exit it). But perhaps I could use some Resume Next
statements If blnStop = True or something...
Anyway, thanks for the reply.