Option to cancel long code execution

M

MariahJ

Hello,

I have developed an optimization tool in VBA using Access as a
back-end. The user clicks an "Optimize" button on a form to execute
code that takes approximately 2 minutes to run. I would like to have an
option to cancel this code execution, but my attempts to do this so far
have not worked.

If tried a "Cancel Optimization" button with the following code in it's
click event:

Private Sub CancelOpt_Click()
Dim canc As New optClass

optClass.gCancel = True

End Sub

The optClass is where the long optimization code is stored. In the
optClass I periodically have checks to see if gCancel is false

If gCancel = False Then 'user has not canceled
' run optimization
Else
GoTo opt_cancel
End If

However, I put a break point in the click event for the "Cancel
Optimization" button and found out the event never executes, so gCancel
always remains true. How can I force this event to execute?

Thanks in advance for your help,
Mariah
 
G

Geoff

Mariah:
However, I put a break point in the click event for
the "Cancel Optimization" button and found out the
event never executes, so gCancel always remains true.
How can I force this event to execute?

Check the On Click property of the cancel button. Does it contain [Event
Procedure]? (This makes Access monitor the event and run your event
procedure.)

If that's not it, try DoEvents in your optimizing code, particularly if
there's a loop in there. You may only need to execute DoEvents, say, once
every 10 loops, to release some processor time.

Geoff
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top