Interrupt VBA execution via mouse click

  • Thread starter Thread starter Sheryl
  • Start date Start date
S

Sheryl

I'd like to have a "Cancel" button on a form which could
interrupt VBA execution. I was able to do this in another
DB language: the program repeatedly called a function
CancelCheck(), which checked if a click event had occurred
on the button. However, when my Access code is running,
the cursor is an hourglass, and nothing can be clicked. Is
there any way I can have this? Ctrl+Break doesn't seem to
do it.
 
Ctrl+Break WILL interupt code execution, however, it may be delayed
depending on the function or method currently being being executed. It is
often necessary to press Ctrl+Break repeatedly in order to interupt a
process. You cannot interupt code via mouse click, as code is being
executed at the same time. You can exit a loop, in which case, you would
need to call DoEvents...then test the flag set by your click event from
within the loop. Something like this:

Private blnClicked As Boolean

Private Sub Cancel_Click()

blnClicked = True

End Sub

Sub TheLooper()

Do until blnClicked = True
DoEvents
Loop

blnClicked = False

End Sub


--
Michael Badnarik for President '04
Libertarian...the freedom party
www.lp.org
www.badnarik.org

"If you are in prison and your chances are 50% for execution by electric
chair, 45% for execution by lethal injection, and 5% for escape, are you
just going to vote for the chair because it is the likeliest outcome?" Vote
Libertarian and live to be free.
 
I was able to do this in another
DB language: the program repeatedly called a function
CancelCheck(), which checked if a click event had occurred
on the button.


' form level private member
private Dim m_fCancelIsClicked as boolean

private sub cmdCancel_Click()
' set the flag
m_fCancelIsClicked = True
End Sub

Private Sub LongSubRoutine

Do While True
' body of loop
DoSomethingHere

' this line enables the GUI to look for a
' mouse click
DoEvents

' now look for the flag
' and leap out if necessary
If m_fCancelIsClicked Then Exit Do

Loop

End Sub
 
Back
Top