|
-
May 28th, 2002, 07:08 PM
#1
Thread Starter
Hyperactive Member
Stopping one Sub from another
Is there some easy way to stop code that is currently being executed if certain keys are pressed (put an if statement in form_keypress)?
I would like to stop some things from happening if certain keys are pressed.
I'm not sure how to do this... say you have a sub that you call and it has code (and triggered events) in it that takes 30 seconds to execute. Well how could you stop and exit this sub using code in form_keypress?
Thanks so much for anyone's help. I'm quite puzzled at the moment.
-
May 28th, 2002, 07:14 PM
#2
Hyperactive Member
If it's a bunch of statements, then use a public flag.
-
May 28th, 2002, 07:21 PM
#3
VB Code:
For x = 1 To 100000000
DoEvents
If mbStopClicked Then
mbStopClicked = False
Exit For
End If
Next
Private Sub cmdStop_Click()
' This is a form-level or global variable
mbStopClicked = True
End Sub
-
May 28th, 2002, 07:25 PM
#4
Thread Starter
Hyperactive Member
I don't have a for loop wrapped around all the code inside the sub. My previous solution was to have a flag set on keypress and check that flag throughout the code, and if it's set, then don't do the next part of the sub. When I look at it, I would be putting in about 30 If statements and it seems like there's a different way.
I guess what I'm asking for is there a way to end a particular sub by referencing it? Like in the forms keypress event (if a certain keypress happens) "End MySub" where 'MySub' is my Subroutine I would like to end.
-
May 28th, 2002, 07:31 PM
#5
Originally posted by wordracr
I guess what I'm asking for is there a way to end a particular sub by referencing it? Like in the forms keypress event (if a certain keypress happens) "End MySub" where 'MySub' is my Subroutine I would like to end.
No
-
May 28th, 2002, 07:35 PM
#6
You could set up a string of keys that u want to detect, like:
VB Code:
For i = 1 to Len(sBuff)
If KeyAscii = Asc(Mid$(sBuff, i, 1)) Then blnStopSub = True
Next
'Or, written another way (One line of Code)
If InStr(sBuff, Chr(KeyAscii)) Then blnStopSub = True
And in ur Sub, as the other guys have previously mentioned:
VB Code:
'
'code
'
If blnStopSub Then [b]Exit Sub[/b]
'
'code
'
Last edited by Bruce Fox; May 28th, 2002 at 08:20 PM.
-
May 28th, 2002, 07:39 PM
#7
Thread Starter
Hyperactive Member
Thank you.
I have all the keypress key combinations coded. I'm planning on putting in if statements in the program so I can stop some things if need be.
Thanks for the assistance
-
May 28th, 2002, 07:48 PM
#8
Do u need If statements?
Can't u set the Global Falg from all the KeyPress_Events?
Then in the Sub If blnFlag Then Exit Sub?
-
May 28th, 2002, 07:56 PM
#9
Thread Starter
Hyperactive Member
Are you saying all the keypress for all controls that my sub handles or one keypress event that handles all control's keypresses?
Hmmm..
Well, how do other programs stop things from happening on a certain keypress? Do they have a bunch of if statements between code?
So there's no way of programming "if at anytime during this sub, bolStop changes to = True, then exit" ?
lol... it seems so simple, yet from what everyone is saying, it is impossible.
-
May 28th, 2002, 08:07 PM
#10
For the pupose of the demonstration, past this on a Form
(Credit to DovvyWeiss and Martin)
VB Code:
Option Explicit
Dim blnStopSub As Boolean
Private Sub Form_Activate()
Looping
End Sub
Private Sub Form_KeyPress(KeyAscii As Integer)
Dim sBuff As String: sBuff = "abcdef"
If InStr(sBuff, Chr(KeyAscii)) Then blnStopSub = True
End Sub
Private Sub Looping()
Dim x As Long
Form1.Caption = "Started Looping"
For x = 1 To 100000000
DoEvents
If blnStopSub Then
blnStopSub = False
Form1.Caption = "Stoped!"
Exit Sub
End If
Next
End Sub
Then press either a, b, c, d, e, or f
Last edited by Bruce Fox; May 28th, 2002 at 08:22 PM.
-
May 28th, 2002, 08:12 PM
#11
BTW, I not suggesting this will solve ur problem, however, teamed
up with a KeyPress API - you may be on ur way
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
|