User Loop Break

By | 2019-09-25

If you have a long-running loop, you might want to give the user the ability to break out of the loop, in case they’ve changed their mind. This bit of code shows how to do that.

Module

Private Declare Function GetAsyncKeyState Lib "user32.dll" _
    (ByVal vKey As Long) As Integer

'//ESCAPE
Const VK_ESCAPE = &H1B
'//Button Click
Const VK_LBUTTON = &H1

Usage

Private Sub cmdStart_Click()
Do
    If GetAsyncKeyState(VK_ESCAPE) < 0 Or _
    GetAsyncKeyState(VK_LBUTTON) < 0 Then
    Debug.Print "-------> EXIT <-------"
    Exit Do
    End If
    Debug.Print "______ LOOPING ___________"
    Loop
End Sub

Author: dwirch

Derek Wirch is a seasoned IT professional with an impressive career dating back to 1986. He brings a wealth of knowledge and hands-on experience that is invaluable to those embarking on their journey in the tech industry.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.