if i have
Sub Command1_Click()
call somethingblah
End Sub
how do i get it to end somethingblah
Printable View
if i have
Sub Command1_Click()
call somethingblah
End Sub
how do i get it to end somethingblah
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...
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 =(
So somethingblah is running in the background and then you want to press command2 to stop it?
yup
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!).
[Edited by seaweed on 03-19-2000 at 09:51 PM]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