• Letting a process run wild

    Author
    Topic
    #471459

    I have a button (wow I hear you say). behind this button I call a procedure in a module.

    It can run for many minutes during which time Access cannot receive any user input.

    I want to be able to cancel the process by allowing the user to click on the button whose caption I changed from ‘Process’ to ‘Cancel’ before I called the procedure

    so clicking on the button changes the caption to “Cancelling” and i have some code in the process like

    Code:
                    DoEvents
                    If ctlCancel.Caption = "Cancelling" Then
                        MsgBox "User Cancelled", vbOKOnly, "Seesion Output"
                        ctlCancel.Caption = "Session Output"
                        GoTo Exit_Procedure
                    End If
    

    So how do I allow the button to be clicked while the process is running?

    Viewing 8 reply threads
    Author
    Replies
    • #1242653

      Having just looked at this thread (but not tried anything) ….

      Does puttting some Do Events into the procedure mean that Access periodically would respond to form events such as clicking the button?

      Does your procedure involve a loop? You could try a DoEvents at the end of each loop.

    • #1242658

      Thanks John but I think the problem is that the click event gives over control to the procedure it calls and waits for the procedure to finish before the control returns to the calling procedure and therefore form. so you can’t click the button whle the procedure is running.

      It’s kinda like when you open a form in code

      docmd.openform “myform”
      ‘ do more stuff …

      the ‘ do more stufff runs and you can return to the form

      but if you open “myform’ modally then code stops until you close “myform”
      and you can’t do anything to the first form until you close “MyForm”

      So I guess I want to call the procedure in such a way that the calling procedure doesn’t wait for it to finish before continuing.

    • #1242665

      Did you look at the thread i pointed to? I thought that, in that thead, Hans said it worked for him.

    • #1242666

      Just shutdown, reopended the app and it now works fine.
      Thanks

    • #1242667

      So if I call a sub then the calling code continues, but if i call a funtion it must wait to get the return value before it can continue.

      Does that sound right?

    • #1242668

      I agree with John.

      I am a bit late on this thread but….

      You cannot call the same procedure to stop it.
      Best way is to have 2 buttons.
      One calls the procedure the other stops it.
      You can place them over each other and have code to hide and unhide.

      I have attached a demo.
      It has just ONE form in it with a loop stopper demo.
      If stop is not pressed it runs for 1 minute.

      On my forms the buttons are apart for clarity of demo, but you could place them on top of each other and then the button caption would appear to change even through a different button.

      It is in Access 2003 format but works fine with 2007 and 2010

      Please Ignore spelling error in Stoped instead of Stopped on finish message

    • #1242672

      Sure Andrew, you can’t call the same procedure to stop it but you can use the same button.
      The original code I posted does work.
      Setting the caption when clicking on the button like this

      Code:
          Set ctl = Screen.ActiveControl
          Select Case ctl.Caption
              Case "Session Output"
                  ctl.Caption = "Cancel"
                  DoEvents
                  ExportSeminarBooking
              Case "Cancel"
                  ctl.Caption = "Cancelling"
              Case Else
          End Select
      

      just seems that I had some sort of lockup that prevented me clicking on the button until I restarted.

    • #1242673

      Maybe I misunderstood issue.
      I can see you code changes the Button Caption, while ExportSeminarBooking is running,
      but it doesn’t actually terminate whatever is running in ExportSeminarBooking or does it?

      • #1242730

        Maybe I misunderstood issue.
        I can see you code changes the Button Caption, while ExportSeminarBooking is running,
        but it doesn’t actually terminate whatever is running in ExportSeminarBooking or does it?

        Code:
          DoEvents
                        If ctlCancel.Caption = "Cancelling" Then
                            MsgBox "User Cancelled", vbOKOnly, "Seesion Output"
                            ctlCancel.Caption = "Session Output"
                            GoTo Exit_Procedure
                        End If
        

        you can see in the above code that when it sees the user cancelled if sets the caption back to where it was in the start and exits the procedure

    • #1242720

      Andrew

      Here is a one button version of your demo. I think that clicking the button when it says “stop” does actually stop the first loop, because when I still had the message in, I got two of them.

    Viewing 8 reply threads
    Reply To: Letting a process run wild

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: