Solved

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

Posted on 2004-08-19
1
183 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
Suggested Courses
Course of the Month8 days, 1 hour left to enroll

617 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