title: How to stop a while with a button on a user interface
body: <p>I'm trying to make a user interface simple with 2 buttons 1 starts a loop and the other one stops it</p>
  
  <p>The only thing I could come up with is to create a global variable that changes depending on which button is pressed and that's the condition for the loop however the stop button won't work while the loop is running and therefore i can't stop the loop</p>
  
  <pre><code>Public Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long
  Public Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
  Public Const MOUSEEVENTF_LEFTDOWN = &amp;H2
  Public Const MOUSEEVENTF_LEFTUP = &amp;H4
  Public Const MOUSEEVENTF_RIGHTDOWN As Long = &amp;H8
  Public Const MOUSEEVENTF_RIGHTUP As Long = &amp;H10
  Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
  Public StartStop As Integer
  Public para As Integer
  
  Sub AutoClick(x)
  
  Do While StartStop = 1
      mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
      mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
      Sleep (x * 1000)
      If para = 0 Then
          Exit Do
      End If
  Loop
  
  
  End Sub
  
  
  Private Sub CommandButton1_Click() 'this is the start button
  
  StartStop = 1
  para = 1
  AutoClick (Segundos)
  
  End Sub
  
  Private Sub CommandButton2_Click() 'this is the stop button
  para = 0
  End Sub
  </code></pre>
  
  <p>Id just like for the stop button to stop the loop, but I haven't come up with an answer yet, don't wanna overcomplicate the code for this</p>