halt your application

Suppose, i have two to command button "cmdGO" and "cmdStop". when i click cmdGo it will run my application but when i click cmdStop to stop the applicaion right then, it wont let me, i have to wait until it goes through all the code in cmdGO. Is there any way to stop the running code right then(like it does when you hit Ctrl+Break. Can any one help me Please?
Mango2Asked:
Who is Participating?
 
Hornet241Connect With a Mentor Commented:
You have to set a global flag on the cmdStop button and then check the flag in the cmdGO code, you also have to let windows process the cmdStop click with the DoEvents()

in the cmdGO sub

cmdGO
Do
    Doevents()
    If cmdStopFlag = True then Exit Sub
    ...
Loop
0
 
dankennedyCommented:
What code do you have in your cmdStop button to make it stop? And do you want the whole program to stop right away? If so, have you tried just using End?
0
 
Arthur_WoodCommented:
yes using End will cause the program to STOP, right then and there....kind of like smashing a car into a brick wall to make it stop, right then and there.  Both however have a bad effect on whatever happens to be going on at the time - for the program, if there are any other resources that are involved, such as other files being open, they REMAIN OPEN, or remin in use.  And for the car, the method of stopping is rather harmful to the presumed occupants of the car at the time.

Hornet241's approach is a much better path to pursue.

AW
0
 
Mango2Author Commented:
Thanks guys, I tried with having a END, but it will terminate the program only after the cmdGo procedure is done.  cmdGo  procedure takes around 30 second to finished its job. It interacts with some other application where it retrieves its data. Once the cmdGO is in process it wont even let me click any other button. Even if i click cmdStop button it will wait for the cmdGO to finished it job and then END the application. What i want is to stop the Application right then once cmdStop is clicked. Any Idea?
0
 
dankennedyCommented:
Like Hornet said, if you put in DoEvents in parts where it could break, or in your loop (if you have one), it should then process the cmdStop button click.

Maybe also try breaking the code in the cmdGo button up, make a couple of different functions which check if a flag is set. This won't stop processing as soon as you hit the button, but it will stop after it's done that section.

Like this...

Private bolFlag as boolean

Private Sub Part1()
if bolFlag then
  < YOUR CODE IN HERE >
end if
End Sub

Private Sub Part2()
if bolFlag then
  < YOUR CODE IN HERE >
end if
End Sub

Private Sub cmdGo_Click()
Call Test1
Call Test2
End Sub
0
All Courses

From novice to tech pro — start learning today.