How to detect the event change from outside the function of the built event?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • subedimite
    New Member
    • Apr 2010
    • 21

    How to detect the event change from outside the function of the built event?

    Hi,

    I have function to generate log files. Depending upon the number of log files that has to be produced; It may take a while to produce all the log files. If I change my mind in the middle about cancelling the process I want to assign the button to stop the process. So what I need to do is while running the Sub “Generatelogs” I need to keep looking if the button has been pressed and then if it has been pressed, I can exit the Sub.

    Is this the right way of doing it? If so how to detect the stop button has been pressed, from outside the function of that button click event?
  • NeoPa
    Recognized Expert Moderator MVP
    • Oct 2006
    • 32636

    #2
    I'm not sure the button press can be detected (in the normal course of events) until after your code has completed execution. An exception might be if you use the command DoEvents.

    Otherwise, the only thing I know that can interrupt the code execution is Ctrl-Break.

    Comment

    • missinglinq
      Recognized Expert Specialist
      • Nov 2006
      • 3533

      #3
      I suspect NeoPa's right (he usually is!) about not being able to use a button here to interrupt execution of your code! I assume if you're generating an unspecified number of log files that you're using some kind of loop, and about the only thing you could do other than his suggestion for Ctrl-Break would be to insert a messagebox at the end of your generating code asking whether or not to continue, and if the answer is No then use an Exit Sub command. Not what you were looking for, but what you may have to settle for.

      Linq ;0)>

      Comment

      • munkee
        Contributor
        • Feb 2010
        • 374

        #4
        Hmmm I did something similar when I was exporting info from an excel file a few years ago.

        I basically set up a Do While, which would check that a variable was true. As soon as I clicked the "End Export" button it would set the variable to false which would exit the sub.

        Comment

        • NeoPa
          Recognized Expert Moderator MVP
          • Oct 2006
          • 32636

          #5
          The logic of that is fine Munkee. The only problem is to allow the code for the interrupt button to execute before the other code has already completed. Did you use DoEvents possibly, to allow it to cede control to the new process?

          Comment

          • subedimite
            New Member
            • Apr 2010
            • 21

            #6
            Thanks for all the Responses.
            - I set up a flag in the stop button click event to detect if it has been pressed ( made a global variable)
            - Used DoEvents on the "Generatelo gs" sub and and checked the status of the flag and exit sub if required

            It is sort of working:) is there a way that I run this Sub in background, so that I can still do other things in the Access at the same time?

            Comment

            • NeoPa
              Recognized Expert Moderator MVP
              • Oct 2006
              • 32636

              #7
              Originally posted by subedimite
              subedimite: Is there a way that I run this Sub in background, so that I can still do other things in the Access at the same time?
              Only if you want to get quite complicated with your code.

              You could arrange for your code to run triggered by the timer event. Each timer event would trigger a single iteration through the loop.

              This is not trivial. Let me say that again. This is not trivial.

              Multi-threaded programming involves various issues that seem to be grasped by very few. If you have no such experience then walk away now. I am not about to try to tutor anyone on such a subject across a web forum. However, the possibility is there if you want to research the issues. I can answer very specific related questions, but that's as far as I'm prepared to go via a medium such as this I'm afraid.

              A further point is that code running in the background would not make for smooth use of Access in the foreground. This would be a far cry from background processing at an OS level.

              Comment

              • subedimite
                New Member
                • Apr 2010
                • 21

                #8
                All right then; Let us not go there at this stage: This not something that is absolutely necessary for me. In fact it is just a bit of curiosity and wanted to give it go. Thanks for you help on other bits.

                Comment

                Working...