Solved

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

Posted on 2004-08-19
1
177 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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now