Search code examples
vbams-accesswhile-looponkeydown

MS Access, DoEvents to exit loop


What I'd like to accomplish:

Do While ctr < List and Break = False
   code that works here...
DoEvents
  If KeyDown = vbKeyQ
      Break = True
  End If
loop

Break out of a loop by holding down a key (eg, Q). I've read up on DoEvents during the loop in order to achieve the functionality that I want. The idea is to have a Do While loop run until either the end of the list is reached or when Q is held down. I'm having issues getting the code to work the way I want, so I'm reaching out to hopefully end the frustration. My experience with VBA is very limited.

UPDATE - More code to expose where the problem might be. This is all in the order I have it (in case order of subs makes a difference:

Private Sub Form_KeyPress(KeyAscii As Integer)
Dim strChar As String

strChar = UCase(Chr(KeyAscii))
If strChar = "Q" Then
    blnQuit = True
    Debug.Print "Q pressed"
End If
End Sub
Private Sub Master_Report_Click()
Dim i As Integer
Dim Deptarray
blnQuit= False

If IsNull(Me.Hospital) Then
    MsgBox ("Please Choose a Hospital")
Else
    DoCmd.OpenForm "Report Print/Update", acNormal, , , , acDialog
    If Report_choice = "Current_List" Then
        Debug.Print "Create master rec report"
        DoCmd.OpenReport "Master Rec Report", acViewPreview
        DoCmd.RunCommand acCmdZoom100
    ElseIf Report_choice = "Update_All" Then
        total = (DCM_Dept.ListCount - 1)
            ctr = 1
            Do While ctr < (DCM_Dept.ListCount) And LoopBreak = False
                Debug.Print "LoopBreak: "; LoopBreak
                Debug.Print "Counter: "; ctr
                DCM_Dept.Value = DCM_Dept.Column(0, ctr)
                Update_Site (Me.Hospital)
                ctr = ctr + 1
                'DoEvents
                   ' If vbKeyQ = True Then
                    'LoopBreak = True
                    'End If
            Loop
            Debug.Print "Update loop exited"
        Debug.Print "Create master rec report"
        DoCmd.OpenReport "Master Rec Report", acViewPreview
        DoCmd.RunCommand acCmdZoom100
    Else
    End If
End If
End Sub

Private Sub Update_Site(Site As String)

If IsNull(Me.Hospital) Then
    MsgBox ("Please Choose a Hospital")
ElseIf IsNull(Me.DCM_Dept) Then
    MsgBox ("Please Choose a Department")
ElseIf Site = "FORES" Then
    Debug.Print "Run FORES update macro"
    DoCmd.RunMacro "0 FORES Master Add/Update"
ElseIf Site = "SSIUH" Then
    Debug.Print "Run SSIUH update macro"
    DoCmd.RunMacro "0 SSIUH Master Add/Update"
End If
End Sub

Report_choice and LoopBreak are both Public variables. My original idea was to have a popup form floating over the main form to display a counter ("Processing department X of Y") and a button to break the loop on there. I realized that the form was unresponsive while the Update_Site() was running its macro so I decided to go with holding a key down instead.

So, where do I go from here to get OnKeyDown to work? Or, is there a better way to do it?


Solution

  • Try to set the Key Preview of the form to Yes and add a variable blnQuit and a key press event in your form like this:

    Private blnQuit As Boolean
    
    'form
    Private Sub Form_KeyPress(KeyAscii As Integer)
        Dim strChar As String
    
        strChar = UCase(Chr(KeyAscii))
        If strChar = "Q" Then
            blnQuit = True
        End If
    End Sub
    

    Then check the blnQuit in your Do While condition, like this:

    blnQuit = False
    Do While ctr < List And Not blnQuit
       code that works here...
       DoEvents
    loop