Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2004-08-19
1
Medium Priority
?
185 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 200 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses

916 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