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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBA - cancel long process 1

Status
Not open for further replies.

steve728

Programmer
Mar 16, 2003
536
US
Can any one out there help me figure out how to to cancel a long process in the middle of a long loop by hitting the esc key in my VBA code. Any help on this matter would be much apprieciated.

Regards,
Steve
 
What about Ctrl+Pause ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Nope! When a user pressess the Esc key I want to provide them with an option to continue or quit and .rollback the process. I have my own Yes/No prompt.

Thanks anyway.

Steve
 
I've heard of the DoEvents line but I need some sample code
please.

Steve
 
By adding a DoEvents as a single line in your loop, the system will check for any events before preceding.

while(true)
(process something)
doevents 'lets other events fire
wend
 
This sample is really vague. Can someone give me more detail please? Remember I want this function to detect if the user hits the Esc key. Actually, any key would be better.

Steve
 
Steve,

Sorry, I was just trying to give an overview. If you set the form to keypreview then the form will test for the key first before any control that has focus. Then if you set the form keydown event to test for the escape key and set the variable isCancel = true. Then in your loop add the

doevents
if( isCancel ) then exit sub

dim isCancel as boolean
Private Sub Form_Load()
Me.KeyPreview = True
End Sub

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
'test if key hit is enter
if((KeyCode = 27) 'Esc Key)
isCancel = true
end if
End Sub

Hope this helps, or gets you going in the right direction.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top