Breaking from a Loop using ESC key


I would like to have the capability to break/cancel routines (recordsets) by pressing the Esc key, especially when looping through large number of records. I would expect to use some function that scans for a key-press at the bottom of the loop and trap the Acsii value of the ESC key. However, I am either trying to do something Access doesn't support or just can't find the correct function. I'm sure that I'm overlooking the solution....



I would recommed to use a form with Cancel button and probably ProgressBar. Enclosed you'll find a template solution for simple popup form with Cancel button...


1. Class module

Private Const mcstrCMyCancel As String = "CMyCancel"

Dim mfrm As Form_frmCancel

Private Sub Class_Initialize()
  Set mfrm = New Form_frmCancel
  mfrm.Visible = True
End Sub

Private Sub Class_Terminate()
  Set mfrm = Nothing
End Sub

Public Property Get CancelClicked() As Boolean
  CancelClicked = mfrm.CancelClicked
End Property

2. Code Behind Form

Private mfCancelClicked As Boolean

Public Property Get CancelClicked() As Boolean
  CancelClicked = mfCancelClicked
End Property
Public Property Let CancelClicked(vf As Boolean)
  mfCancelClicked = vf
End Property

Private Sub cmdCancel_Click()
  Me.CancelClicked = True
End Sub

3. Test

Public Sub EndlessLoop()
  Dim obj As New CMyCancel

  Do While 1
    If obj.CancelClicked Then
      Exit Do
    End If

  Set obj = Nothing

End Sub

Copyright 1999-2008 by Shamil Salakhetdinov.  All rights reserved.