Results 1 to 6 of 6

Thread: end function

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2000
    Posts
    8
    if i have

    Sub Command1_Click()
    call somethingblah
    End Sub


    how do i get it to end somethingblah

  2. #2
    Addicted Member
    Join Date
    Jan 2000
    Location
    Sydney, Australia
    Posts
    196
    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...


  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2000
    Posts
    8
    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 =(

  4. #4
    Addicted Member
    Join Date
    Jan 2000
    Location
    Sydney, Australia
    Posts
    196
    So somethingblah is running in the background and then you want to press command2 to stop it?

  5. #5

    Thread Starter
    New Member
    Join Date
    Mar 2000
    Posts
    8
    yup

  6. #6
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Bellevue, WA, USA
    Posts
    1,357

    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]
    ~seaweed

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width