Interrupting a loop with a button click

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi, everyone!

[Access 2002]
I have a for/next loop that does some serious data manipulation on tables
containing many thousands of records. Occasionally, I want it to stop
processing after several hundred records have been done to check the results,
so I added a "Stop" button to the form to keep me from having to update the
code manually to stop after x records are processed. Just before I start the
loop, I make the button visible, then turn it off when the loop is finished.

I tried using the button to set the loop counter (x) to the "To" value (y)
in the "For x = 1 To y" statement. That did not work.

Next, I tried creating a global variable that is tested just prior to the
"Next x" statement, and set it to False using the button. That didn't work
either.

It's like clicking the button doesn't update the variable, but if I break
into the code, the value is there. (PS: It takes a few seconds for
processing to stop when I hit Ctrl+Break.) If I step through the code, it
works fine.

Does anyone know a neat trick to make this work?

Thanks,
BruceS
 
Hi Bruce

Have the button set a module-level boolean variable to True in its Click
event procedure. Then the trick is to insert a DoEvents statement in your
loop, just before you check whether this boolean has been set. That will
allow any outstanding events (i.e. your Click event) to fire before the code
continues.
 
Thanks, Graham! Worked great!
Bruce

Graham Mandeno said:
Hi Bruce

Have the button set a module-level boolean variable to True in its Click
event procedure. Then the trick is to insert a DoEvents statement in your
loop, just before you check whether this boolean has been set. That will
allow any outstanding events (i.e. your Click event) to fire before the code
continues.
--
Good Luck!

Graham Mandeno [Access MVP]
Auckland, New Zealand

BruceS said:
Hi, everyone!

[Access 2002]
I have a for/next loop that does some serious data manipulation on tables
containing many thousands of records. Occasionally, I want it to stop
processing after several hundred records have been done to check the
results,
so I added a "Stop" button to the form to keep me from having to update
the
code manually to stop after x records are processed. Just before I start
the
loop, I make the button visible, then turn it off when the loop is
finished.

I tried using the button to set the loop counter (x) to the "To" value (y)
in the "For x = 1 To y" statement. That did not work.

Next, I tried creating a global variable that is tested just prior to the
"Next x" statement, and set it to False using the button. That didn't
work
either.

It's like clicking the button doesn't update the variable, but if I break
into the code, the value is there. (PS: It takes a few seconds for
processing to stop when I hit Ctrl+Break.) If I step through the code, it
works fine.

Does anyone know a neat trick to make this work?

Thanks,
BruceS
 
Back
Top