|
-
Dec 10th, 2009, 11:29 AM
#1
Thread Starter
New Member
End sub from another sub?
Sorry if the title is confusing. Here is what I need to do:
Start sub1 in form1
---->if user clicks a button on form2 it needs to end sub1 on form1
I may be missing an easy solution here, but i need to halt all instructions on form1 if the button is clicked.
Any help is appreciated.
-
Dec 10th, 2009, 11:38 AM
#2
Re: End sub from another sub?
What is sub1 one doing? Is it looping and you need to cancel the loop?
-
Dec 10th, 2009, 11:56 AM
#3
Re: End sub from another sub?
 Originally Posted by bighairycamel
Start sub1 in form1
---->if user clicks a button on form2 it needs to end sub1 on form1
...
How do you open Form2?
If it's a modal form then you can pass some value back to Form1 when Form2 gets unloaded and based on that value (could be Yes/No, True/False, 1/0, etc...) you'd know whether you need to exit or continue.
-
Dec 10th, 2009, 12:06 PM
#4
Re: End sub from another sub?
Here is a quick sample:
- say you show From2 as modal and Form2 has 2 button (OK and Cancel)
- if OK button was clicked you would proceed with your code
- if Cancel button was clicked you'd exit procedure on Form1
Code:
'Form1:
Private Sub Command1_Click()
Dim proceed As Boolean
Form2.Show vbModal
proceed = Form2.blnOK
Unload Form2
If Not proceed Then
MsgBox "Sorry! I was told to exit..."
Exit Sub
End If
'otherwise continue with you code...
MsgBox "Hello! I was told to continue..."
End Sub
'Form2:
Option Explicit
Public blnOK As Boolean
Private Sub btnCancel_Click()
blnOK = False
Me.Hide
End Sub
Private Sub btnOK_Click()
blnOK = True
Me.Hide
End Sub
-
Dec 10th, 2009, 12:19 PM
#5
Thread Starter
New Member
Re: End sub from another sub?
 Originally Posted by Hack
What is sub1 one doing? Is it looping and you need to cancel the loop?
Basically I'm emulating keystrokes to automate certain functions of another program. It takes about 30 seconds for the process to complete, but I need to be able to have the user cancel the code if needed. So I have a form sit on top of all windows with a "halt" command button.
Rhino: I need the code from form1 to continue processing until a button is clicked. Unless I am misunderstanding, it looks like your code will only let the code process after the button is clicked.
I had already tossed around the idea of making a public boolen and having form1 check every few lines of code for the value. But that's rather tedious but if there is no simpler way it's not a big deal.
-
Dec 10th, 2009, 12:25 PM
#6
Re: End sub from another sub?
Are your keystrokes being performed one right after the other without any type of break? Or are their breaks, whether via a Sleep function or possibly a timer used to send the strokes? A bit more detail would be helpful.
-
Dec 10th, 2009, 12:38 PM
#7
Thread Starter
New Member
Re: End sub from another sub?
Yes there is a Pause function called from a module between keystrokes. The user can adjust a setting to make the pauses shorter or longer effectively changing the speed of the program, but generally the pauses are anywhere from .1 - 1 second.
And also I have DoEvents in several key places in the code.
-
Dec 10th, 2009, 12:47 PM
#8
Re: End sub from another sub?
FYI: Sprinkling DoEvents indiscriminately may produce unexpected problems.
Here is one method:
1. At top of your form1, declare a PUBLIC boolean variable named, possibly, bAbortStrokes
2. Just before your keystroke routine starts, set that abort variable to False
3. After each pause command, check that variable to see if it changed and exit if so:
i.e., If bAbortStrokes = True Then Exit Sub
4. In your form1 command button's click event, set the abort variable to True
i.e., form1.bAbortStrokes = True
If DoEvents are called from within your pause function or after a subset of keystrokes occur, that may be good enough. If not, you may want to add a DoEvents before your Pause function exits. The DoEvents allow the command button to be clicked while your keystroke routine is running.
Edited: I didn't catch that the button and function are in different forms. Modified comments above.
Last edited by LaVolpe; Dec 10th, 2009 at 12:57 PM.
-
Dec 10th, 2009, 12:53 PM
#9
Re: End sub from another sub?
 Originally Posted by bighairycamel
...Rhino: I need the code from form1 to continue processing until a button is clicked. Unless I am misunderstanding, it looks like your code will only let the code process after the button is clicked...
No, you understood correctly - I only posted quick sample.
However, in your "real world" you can simply raise some boolean flalg on Form2 at some point but Form1 needs "detector" (perhaps If - End If before each block of code) - if flag is up exit procedure.
-
Dec 10th, 2009, 12:56 PM
#10
Thread Starter
New Member
Re: End sub from another sub?
Thanks. That's exactly what I had in mind when I posted this topic but I was just hoping there was a simpler way to exit the sub. Oh well, I'll just be thankful for ctrl+v.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|