Results 1 to 11 of 11

Thread: Stopping one Sub from another

  1. #1

    Thread Starter
    Hyperactive Member wordracr's Avatar
    Join Date
    Aug 2001
    Posts
    281

    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.

  2. #2
    Hyperactive Member DovyWeiss's Avatar
    Join Date
    Feb 2002
    Location
    Miami Beach, FL
    Posts
    366
    If it's a bunch of statements, then use a public flag.

  3. #3
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    VB Code:
    1. For x = 1 To 100000000
    2.     DoEvents
    3.     If mbStopClicked Then
    4.         mbStopClicked = False
    5.         Exit For
    6.     End If
    7. Next
    8.  
    9. Private Sub cmdStop_Click()
    10.  
    11.     ' This is a form-level or global variable
    12.     mbStopClicked = True
    13.  
    14. End Sub

  4. #4

    Thread Starter
    Hyperactive Member wordracr's Avatar
    Join Date
    Aug 2001
    Posts
    281
    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.

  5. #5
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    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

  6. #6
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    You could set up a string of keys that u want to detect, like:
    VB Code:
    1. For i = 1 to Len(sBuff)
    2.  If KeyAscii = Asc(Mid$(sBuff, i, 1)) Then blnStopSub = True
    3. Next
    4.  
    5. 'Or, written another way (One line of Code)
    6.  
    7.     If InStr(sBuff, Chr(KeyAscii)) Then blnStopSub = True

    And in ur Sub, as the other guys have previously mentioned:
    VB Code:
    1. '
    2. 'code
    3. '
    4.   If blnStopSub Then [b]Exit Sub[/b]
    5. '
    6. 'code
    7. '
    Last edited by Bruce Fox; May 28th, 2002 at 08:20 PM.

  7. #7

    Thread Starter
    Hyperactive Member wordracr's Avatar
    Join Date
    Aug 2001
    Posts
    281
    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

  8. #8
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    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?

  9. #9

    Thread Starter
    Hyperactive Member wordracr's Avatar
    Join Date
    Aug 2001
    Posts
    281
    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.

  10. #10
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    For the pupose of the demonstration, past this on a Form
    (Credit to DovvyWeiss and Martin)

    VB Code:
    1. Option Explicit
    2.  
    3. Dim blnStopSub As Boolean
    4.  
    5. Private Sub Form_Activate()
    6.    Looping
    7. End Sub
    8.  
    9. Private Sub Form_KeyPress(KeyAscii As Integer)
    10.  
    11.    Dim sBuff As String: sBuff = "abcdef"
    12.  
    13.    If InStr(sBuff, Chr(KeyAscii)) Then blnStopSub = True
    14.  
    15. End Sub
    16.  
    17. Private Sub Looping()
    18.    Dim x As Long
    19.  
    20.    Form1.Caption = "Started Looping"
    21.  
    22.    For x = 1 To 100000000
    23.        DoEvents
    24.        If blnStopSub Then
    25.            blnStopSub = False
    26.            Form1.Caption = "Stoped!"
    27.            Exit Sub
    28.        End If
    29.    Next
    30. End Sub

    Then press either a, b, c, d, e, or f
    Last edited by Bruce Fox; May 28th, 2002 at 08:22 PM.

  11. #11
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    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
  •  



Click Here to Expand Forum to Full Width