Solved

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

Posted on 2004-08-19
1
180 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

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

821 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