Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 287
  • Last Modified:

how to enable a user to interrupt a loop without entering debug mode

I have a loop in my code that checks for data that another process is creating. I
check for the data,
if it's not found then increment a counter variable,
wait 6 seconds
start at the top of the loop until the counter reaches 8
then prompt to user to try the loop again or exit the loop without the data.

I'd like to come up with a way to enable the user to interrupt the loop instead of having to wait for the 48 seconds for the loop to process. Anyone have a good way to do that?
0
GordonPrince
Asked:
GordonPrince
  • 7
  • 4
1 Solution
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
Basically you can do this:

1) put a check box on your form ... say chkStopLoop

2) in your code ....

    DoEvents    ' let Windows catch up
    If Me.chkStopLoop = True then Exit For  '  or Exit Do or what ever

this is the basic idea ...

mx
0
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
You might even:

    DoEvents    ' let Windows catch up
    If Me.chkStopLoop = True then
          If (Msgbox "Stop Loop?") = vbYes Then  ' ask user
              Exit For  '  or Exit Do or what ever
          End If
    Endif

mx
0
 
rockiroadsCommented:
have you considered using a timer? if this is on a form, configure your form timer to be 6 seconds. Your code that does incrementing etc will then go or get called form form_timer function

Now have a button on your form, call it stop or whatever which will disable the form timer, which then means your code called from form_timer will never get called

Me.TimerInterval = 6000   'sets code in Form_Timer to run every 6 seconds

Me.TimerInterval = 0   'cancel timer
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
GordonPrinceAuthor Commented:
I'm using a public function that gets called from more than one form. I'd like to some up with something that the public function responds to, if possible. Otherwise it looks like I would have to duplicate the logic on each form that calls the function.
0
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
Can you post the code for the function?

mc
0
 
GordonPrinceAuthor Commented:
Function PollJavelan4Invoice(strMatNo As String, intTaxYr As Integer, strParNo As String) As Long
  Const strTitle As String = "Poll Javelan for an Invoice"
  Dim conJav As New adodb.Connection, rst As adodb.Recordset
  Dim strCID As String, strMID As String, strText As String
  Dim bytCounter As Byte

On Error GoTo cmdWait_Error
   
    strText = "%" & intTaxYr & " -- Parcel " & strParNo & "%"
    If Not ValidCM(strMatNo, strCID, strMID) Then Exit Function
    conJav.Open strConJav

Wait4Invoice:
    DoCmd.Hourglass True
    For bytCounter = 1 To 8
        Set rst = conJav.Execute("sp_InvoiceNo4PTG '" & strCID & "', '" & strMID & "', '" & strText & "'")
        With rst
            If .EOF Then
            Else
                If .Fields("BLSTMTDT") = Date Then
                    strScratch = "Use today's InvoiceNo " & .Fields("BLSTMTNUM")
                    If MsgBox(strScratch & "?", vbYesNo, strTitle) = vbYes Then PollJavelan4Invoice = .Fields("BLSTMTNUM")
                    GoTo cmdWait_Exit
                End If
            End If
            .Close
        End With
        Set rst = Nothing
        If bytCounter = 1 Then
            SysCmd acSysCmdInitMeter, "Checking Javelan for Invoice", 10
        Else
            SysCmd acSysCmdUpdateMeter, bytCounter
        End If
        Sleep 6000
    Next
    SysCmd acSysCmdRemoveMeter
    DoCmd.Hourglass False
    If MsgBox("Poll Javelan for another 45 seconds?", vbQuestion + vbYesNo, strTitle) = vbYes Then GoTo Wait4Invoice
   
cmdWait_Exit:
    DoCmd.Hourglass False
    Set conJav = Nothing
    Exit Function
   
cmdWait_Error:
    MsgBox Err.Description, vbExclamation, strTitle
    If Ok2Debug Then
        Stop
        Resume
    End If
End Function
0
 
GordonPrinceAuthor Commented:
I'd like to check for an interrupt from the user after the Sleep 6000 statement, I think.
0
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
"after the Sleep 6000 statement"

If you use the concept in my first post, you can do that at this point.

mx
0
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:

......
      Else
            SysCmd acSysCmdUpdateMeter, bytCounter
        End If
        Sleep 6000
        DoEvents    ' let Windows catch up
         If Me.chkStopLoop = True then
             
cmdWait_Exit:
0
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
Sorry ... bad Submit
Using the idea from my first post:

......
      Else
            SysCmd acSysCmdUpdateMeter, bytCounter
        End If
        Sleep 6000
        DoEvents    ' let Windows catch up
         If Me.chkStopLoop = True then
              ' maybe a confirmation message/question here    
               Goto cmdWait_Exit
        End If

mx
0
 
GordonPrinceAuthor Commented:
That did it.

To make it work from a Public Function I passed in the form that called it as a parameter. Then in the function I checked frm.Controls("chkStopLoop"), etc.

Just to smooth out the user experience, I also made the check box control + label + bright red colored box around the two of them not visible. When the function with the loop is called, the first thing I do is make the controls visible. Then hide them again at the end of the function.

Thanks for the help.
0
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
Excellent. good job.

thank you ...

mx
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 7
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now