|
-
Mar 19th, 2000, 08:50 AM
#1
Thread Starter
New Member
if i have
Sub Command1_Click()
call somethingblah
End Sub
how do i get it to end somethingblah
-
Mar 19th, 2000, 08:55 AM
#2
Addicted Member
somethingblah should be defined as
sub somethingblah()
...
...
...
end sub
the end here will end the sub. but if you want to end it somewhere in the code, you can use exit sub, eg:
sub somethingblah()
...
...
if bShouldWeGoOn = False then
exit sub
end if
...
...
end sub
i hope this is what you meant, because i'm not 100% sure what you were asking...
-
Mar 19th, 2000, 09:01 AM
#3
Thread Starter
New Member
oops, ok, i left out something very important
ok...first there is
Private Sub Command1_Click()
call somethingblah
End Sub
now i want to end something blah by the click of
Command2_Click
so umm....i'm not sure what to do =(
-
Mar 19th, 2000, 09:08 AM
#4
Addicted Member
So somethingblah is running in the background and then you want to press command2 to stop it?
-
Mar 19th, 2000, 09:21 AM
#5
Thread Starter
New Member
-
Mar 19th, 2000, 09:42 AM
#6
Frenzied Member
Would this work?
If the subprocedure that you want to end using a command button is running in a loop, simply have the loop dependant on the value of a module-level variable.
Say you have a module level variable (declared Private in General Declarations or Public in a Module) that is of type Boolean. Let's call it "mbRunLoop" and set it to true when the form loads. Then in your sub procedure, have your loop run as long as mbRunLoop is TRUE (and whatever other conditions you may already be testing for).
Then, when the user clicks your command button, simply have code that changes mbRunLoop to FALSE. That way the loop condition will fail next time it tries to run and your sub procedure will stop.
Here's a small sample program...
Start a new project and drop a label and a command button on the form. Then paste the following code into the form's code window. When you run the project, the label will display a forever-changing number. The only way to stop it is to click the command button (you cant even click the 'x' to stop it!).
Code:
Option Explicit
Private mbRunLoop As Boolean
Private Sub Command1_Click()
mbRunLoop = False 'This will make loop end
End Sub
Private Sub Form_Load()
Dim nCounter As Integer
mbRunLoop = True 'Set module level variable to TRUE
Me.Show
Do While mbRunLoop = True
'Increment the counter
nCounter = nCounter + 1
'Dont let counter overflow...reset it to 1
If nCounter > 32766 Then
nCounter = 1
End If
Label1.Caption = nCounter
'Make sure you include DoEvents so your program doesn't freeze!
DoEvents
Loop
End Sub
[Edited by seaweed on 03-19-2000 at 09:51 PM]
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
|