• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 293
  • 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 Access MVP)Database ArchitectCommented:
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 Access MVP)Database ArchitectCommented:
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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
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 Access MVP)Database ArchitectCommented:
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 Access MVP)Database ArchitectCommented:
"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 Access MVP)Database ArchitectCommented:

......
      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 Access MVP)Database ArchitectCommented:
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 Access MVP)Database ArchitectCommented:
Excellent. good job.

thank you ...

mx
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

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