Solved

How to stop process thats running inside private sub by clicking on cmdbutton?

Posted on 2004-08-19
1
179 Views
Last Modified: 2010-05-02
Hi!
The problem is:

I have private sub and inside that sub there isa code which access multiple files and make some operations on them, (renaming, compressing etc...)
I would like to give the user possibility to stop the process by clicking on the command button and poping out the message box vbyesno and if yes then exit sub.
I tried with Do While, but when i click the Command button program freeze. (CTRL+BREAK is the only thing that can stop program)

Any idea would be very welcomed!

I am sorry for my english but it's not my native language :)
0
Comment
Question by:marino123
1 Comment
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 50 total points
ID: 11839008
You need two things.

1)  You need a DoEvents inside your loop otherwise the click on the button won't be processed.

2) You need a boolean flag, delcared in the General Declarations section of your form, to control processing.

The general precess is something like:


    Private m_bProcess As Boolean      '// In general declarations section

    Private Sub cmdCancel_Click()

        If Msgbox("Do you want to cancel ?", vbYesNo) = vbYes Then
            m_bProcess = False     '// Set flag to stop processing
        End If

    End Sub


    Private Sub YourMainProcessingLoop()

        m_bProcess = True         '// Default value

        Do While SomeCondition

              DoEvents       '// Need this so user can actually click "cancel" button

              '// Check for user having cancelled
              If Not m_bProcess Then Exit Do

              '//   User hasn't cancelled so do some stuff
 
        Loop

    End Sub


Hope this helps.
0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question