473,837 Members | 1,705 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to stop running sub routine?

I have a sub routine that searches the Outlook PST for a message sent
to/from a particular email address. Obviously, this can take a long time
when the PST contains thousands of messages. I'd like to put a button on my
Access 20003 form that will stop the sub routine when clicked. I know I can
press Ctl + Break and stop the code, but is there a more graceful way to do
this programmaticall y? Should I use SendKeys?

Also, it would be nice to have the datasheet (that shows the results of the
email search) populate with the found message headers as they are found -
I'm still thinking about how to do this.... any thoughts/suggestions are
very welcome!!

thanks!
Nov 12 '05 #1
16 12671
On Sun, 11 Jan 2004 07:34:20 GMT in comp.databases. ms-access, "deko"
<dj****@hotmail .com> wrote:
I have a sub routine that searches the Outlook PST for a message sent
to/from a particular email address. Obviously, this can take a long time
when the PST contains thousands of messages. I'd like to put a button on my
Access 20003 form that will stop the sub routine when clicked. I know I can
press Ctl + Break and stop the code, but is there a more graceful way to do
this programmaticall y? Should I use SendKeys?

Also, it would be nice to have the datasheet (that shows the results of the
email search) populate with the found message headers as they are found -
I'm still thinking about how to do this.... any thoughts/suggestions are
very welcome!!


20003 eh? Wow, can't wait for that one <g>

I assume your code has some kind of loop? e.g.

Do while <someconditio n>
.. doing messages
Loop

If this is in a form then create a module level variable (in the
delcations section, e.g.

Dim mfSearching as Boolean and have a "stop" button on that form

or if in a public module then a public or global variable, then use a
popup form with a "stop" button on it. The Stop button's code will
look like...

Sub cmdStop_Click()
mfSearching = False
End Sub

Now back to your main loop, it should do something along the lines
of...

mfSearching = True
Do while (<somecondition >) And mfSearching
.. doing messages
Doevents
Loop

You need the DoEvents to allow the stop button to be pressed, when
pressed the module/global variable mfSearching is set to false and the
main loop will end natrually.

I've often done similar things and scaled down the scope of the
variables needed by re-using the start button, e.g.

Sub cmdStart_Click( )
Static fRunning As Boolean ' note must be static
if fRunning Then
fRunning=False
cmdStart.Captio n="Stopping"
DoEvents
exit sub
end if
fRunning = True
cmdStart.Captio n="Stop"
Do while <someconditio n> And fRunning
... do something
DoEvents
Loop
cmdStart.Captio n="Start"
End Sub

Note if using a For loop, then insert some code inside the loop like:

if not mfRunning Then
Exit For
End If

The same can be used for do loops as well (using "exit do") in case
there's a lot of things to do each iteration.

--
A)bort, R)etry, I)nfluence with large hammer.
Nov 12 '05 #2
> Note if using a For loop, then insert some code inside the loop like:

if not mfRunning Then
Exit For
End If


It's a "For Each ... Next" loop.

so... the code to add is:

Dim mfRunning as Boolean

then inside the For Each loop:

if not mfRunning Then
Exit For
End If

and on the cmdStart_Click button, something like this:

Sub cmdStart_Click( )
Static fRunning As Boolean
if fRunning Then
fRunning=False
cmdStart.Captio n="Stopping"
DoEvents
exit sub
end if
fRunning = True
cmdStart.Captio n="Stop"
For Each ...
... do something
DoEvents
Next
cmdStart.Captio n="Start"
End Sub

I'll have to play around with this a bit... thanks for pointing me in the
right direction...
Nov 12 '05 #3
at the top of your form's code, add
dim blnStop as boolean

before starting your search
blnStop = false

the 'click' event of your STOP button
blnStop = true

in your search loop
if (blnstop) then
exit sub
end if

.....

create a table called tblMessagesFoun d with the fields that you want
to see in your datasheet

before running the search
delete * from tblMessagesFoun d

while search, add new messages to the table
dim rs as recordset

set rs = currentdb.openr ecordset("tblMe ssagesFound")
your loop
if something found
rs.addNew
rs!message = ......
rs.update
end if
end of your loop

rs.close
set rs = nothing
create a datasheet form with tblMessagesFoun d as the source
"deko" <dj****@hotmail .com> wrote in message news:<00******* **********@news svr25.news.prod igy.com>...
I have a sub routine that searches the Outlook PST for a message sent
to/from a particular email address. Obviously, this can take a long time
when the PST contains thousands of messages. I'd like to put a button on my
Access 20003 form that will stop the sub routine when clicked. I know I can
press Ctl + Break and stop the code, but is there a more graceful way to do
this programmaticall y? Should I use SendKeys?

Also, it would be nice to have the datasheet (that shows the results of the
email search) populate with the found message headers as they are found -
I'm still thinking about how to do this.... any thoughts/suggestions are
very welcome!!

thanks!

Nov 12 '05 #4
No,

use "DoEvents" (see helpfile for details)

Hans Heezemans
www.iservi.com

"deko" <dj****@hotmail .com> schreef in bericht
news:00******** *********@newss vr25.news.prodi gy.com...
I have a sub routine that searches the Outlook PST for a message sent
to/from a particular email address. Obviously, this can take a long time
when the PST contains thousands of messages. I'd like to put a button on my Access 20003 form that will stop the sub routine when clicked. I know I can press Ctl + Break and stop the code, but is there a more graceful way to do this programmaticall y? Should I use SendKeys?

Also, it would be nice to have the datasheet (that shows the results of the email search) populate with the found message headers as they are found -
I'm still thinking about how to do this.... any thoughts/suggestions are
very welcome!!

thanks!

Nov 12 '05 #5
Here's what I've tried... (not working, but getting close, I think...)

This code is in a standard module. The form with the buttons is
frmEmailMessage s.

Public Sub SearchPST()
Dim blnStop As Boolean
If Forms!frmEmailM essages!cmdStar tSearch.Caption = "Stop Search" Then
blnStop = True
Else
blnStop = False
End If
[code omitted]
Do Until rst.EOF
Forms!frmEmailM essages!cmdStar tSearch.Caption = "Stop Search"
For Each olmi In olFolder.Items
DoEvents ' note: DoEvents must be here - can't be in the "Do
.... Loop" or other code will not work.
If (blnStop) Then GoTo Stop_Here
[code omitted]
Next
Loop
Stop_Here:
Forms!frmEmailM essages!cmdStar tSearch.Caption = "Start Search"
rst.Close
Set olmi = Nothing
Set olFolder = Nothing
End Sub

I was hoping I could use the "If ... Else" statement to toggle blnStop - but
clicking on cmdStartSearch (with caption "Stop Search") does not stop the
routine. For some reason the second time I click the button, the event is
not processed. I'm wondering if I need to put the code in frmEmailMessage s
rather than in a separate module ???

Nov 12 '05 #6

Here's what I've tried... (not working, but getting close, I think...)

*** I've also tried using a sepatare cmdStop button (which I think is what I
want to do), but that did not work either....
I must still be missing something....

This code is in a standard module. The form with the buttons is
frmEmailMessage s.

Public Sub SearchPST()
Dim blnStop As Boolean 'also tried Static blnStop here but no luck...
If Forms!frmEmailM essages!cmdStar tSearch.Caption = "Stop Search" Then
blnStop = True
Else
blnStop = False
End If
[code omitted]
Do Until rst.EOF
Forms!frmEmailM essages!cmdStar tSearch.Caption = "Stop Search"
For Each olmi In olFolder.Items
DoEvents ' note: DoEvents must be here - can't be in the "Do
.... Loop" or other code will not work.
If (blnStop) Then GoTo Stop_Here
[code omitted]
Next
Loop
Stop_Here:
Forms!frmEmailM essages!cmdStar tSearch.Caption = "Start Search"
rst.Close
Set olmi = Nothing
Set olFolder = Nothing
End Sub

I was hoping I could use the "If ... Else" statement to toggle blnStop - but
clicking on cmdStartSearch (with caption "Stop Search") does not stop the
routine. For some reason the second time I click the button, the event is
not processed. I'm wondering if I need to put the code in frmEmailMessage s
rather than in a separate module ???

Nov 12 '05 #7
On Sun, 11 Jan 2004 22:30:25 GMT in comp.databases. ms-access, "deko"
<dj****@hotmail .com> wrote:
Here's what I've tried... (not working, but getting close, I think...)

This code is in a standard module. The form with the buttons is
frmEmailMessag es.

Public Sub SearchPST()
Dim blnStop As Boolean
If Forms!frmEmailM essages!cmdStar tSearch.Caption = "Stop Search" Then
blnStop = True
Else
blnStop = False
End If
[code omitted]
Do Until rst.EOF
Forms!frmEmailM essages!cmdStar tSearch.Caption = "Stop Search"
For Each olmi In olFolder.Items
DoEvents ' note: DoEvents must be here - can't be in the "Do
... Loop" or other code will not work.
If (blnStop) Then GoTo Stop_Here
[code omitted]
Next
Loop
Stop_Here:
Forms!frmEmailM essages!cmdStar tSearch.Caption = "Start Search"
rst.Close
Set olmi = Nothing
Set olFolder = Nothing
End Sub

I was hoping I could use the "If ... Else" statement to toggle blnStop - but
clicking on cmdStartSearch (with caption "Stop Search") does not stop the
routine. For some reason the second time I click the button, the event is
not processed. I'm wondering if I need to put the code in frmEmailMessage s
rather than in a separate module ???


Your code is re-entrant, basically when you press the button a second
time to stop it, it runs through the procedure again but exits out of
the loop straight away, the original call to the routine carries on
regardless. You should make blnStop a static variable and exit the sub
when you set it to true, the original call will then exit the loop.

You can also avoid the use of the dreaded "goto"...
blnStop = False ' <Note[1]
Do Until rst.EOF or blnStop
For Each ...
If blnStop Then
Exit For
end if
Next
rst.Movenext
Loop

[1] You need to set this to false prior to your loop as well as
exiting the sub before that when setting it to true otherwise once you
have pressed your stop button, the code will never run again.

--
A)bort, R)etry, I)nfluence with large hammer.
Nov 12 '05 #8
okay, here's the latest - but the problem still seems to be setting blnStop
to True - the cmdStop button just doesn't seem to do it... do I have to put
the code from modOutlook into frmEmailMessage s?
thanks again for the help!

[modOutlook] - a standard module

Public Sub SearchPST()
Static blnStop As Boolean
blnStop = False
[code omitted]
Do Until rst.EOF or blnStop
For Each olmi In olFolder.Items
DoEvents
If (blnStop) Then Exit For
[code omitted]
Next
Loop
Stop_Here:
rst.Close
Set olmi = Nothing
Set olFolder = Nothing
End Sub

[frmEmailMessage s] - a popup form

Option Compare Database
Option Explicit
Public blnStop As Boolean

Private Sub cmdStop_Click() 'note [1]
blnStop = True
End Sub

note [1] this is not the same button that launches searchPST, it's in a
separate form module
Nov 12 '05 #9
> Your code is re-entrant, basically when you press the button a second
time to stop it, it runs through the procedure again but exits out of
the loop straight away, the original call to the routine carries on
regardless. You should make blnStop a static variable and exit the sub
when you set it to true, the original call will then exit the loop.


This seems to be working... but I think I will have to use GoTo because
there is some code that fires (that I don't want to fire) when the sub is
called from cmdStop. Thanks again for the help!
[frmEmailMessage s] - the form with the controls

Private Sub cmdStart()
modOutlook.Sear chPST (False)
End Sub

Private Sub cmdStop_Click()
modOutlook.Sear chPST (True)
End Sub

[modOutlook] - a standard module with the following code

Public Sub SearchPST (blnStop as Boolean)
[code omitted]
Do Until rst.EOF or blnStop
DoEvents <<== this is needed here
For Each olmi In olFolder.Items
DoEvents <<== as well as here
If (blnStop) Then Exit For
[code omitted]
Next
Loop
Stop_Here:
rst.Close
Set olmi = Nothing
Set olFolder = Nothing
End Sub
Nov 12 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
7830
by: lawrence | last post by:
Suppose I create dynamic web pages with 3 functions (which call other functions to make everything happen, but these 3 you might think of as being the top layer): registerSessions(); sendHtmlToBrowsers(); incrementPageViews(); Is there any chance that incrementPageViews() will be executed? Or, to
6
1672
by: Perttu Pulkkinen | last post by:
I just want to ask php-community, are they running too fast with new versions. Why: 1) Its not so nice to load 4.2x and then come back couople of months later to find out that php 5 is out while you're still making yourself familiar with 4.2x. 2) Are webhosters happy to announce their clients "btw, we have installed new version, if your applications stop running, that maybe the reason, but figure out yourself"?
4
10199
by: PalB | last post by:
How to stop running multiple instances of my App in C#? Thanx
2
18603
by: David Hunt | last post by:
I'm developing a Windows Service and just added a standard try... catch exception handler to its OnStart() routine. I would like to know how to force the service to abort its startup when an exception is encountered. In my current version, the exception handler logs the exception, but then the service continues to run. I'd like it to NOT run after logging the exception. Thanks!
1
2983
bhing
by: bhing | last post by:
hiya!!! Im assigned to a website where jsp,mysql, and tomcat is used which runs on fedora 5. NOw i wonder why sometimes the tomcat will stop running, so i have to restart it again to keep the website up and running. BUt that's not only the problem because sometimes even if i already restarted the tomcat, nothing happens, so i restarted the computer to make it work.. Can anybody help me trace where the problem is?? OR can anybody give me all...
1
3508
by: pssheba | last post by:
Hi, My sp creates an error report. at a certain stage i want it to stop running if there are errors in thar report. I wrote: DECLARE @myparam INTEGER SELECT @myparam=COUNT(*) FROM report_errors1 IF (@myParam>0) BEGIN print 'stop running due to existing errors ! '
2
2449
Maidenz08
by: Maidenz08 | last post by:
Hi, Guess this is a trivial question but am simply not able to get it. I have created a exe for my windows application(c#,.net,frameworks 2.0). It is scheduled to run at particular intervals of time. After its complete i need to stop it. I have tried using application.exit(), application.exitthread() but to no avail. After these lines are executed my visual studio shows that it is still running. How do i stop it? Thanks in advance...
33
11640
by: Sunny | last post by:
Hi, Sometime, when your script is too big, IE Gives you a warning "Stop Running This Script" A script on this page is causing Internet Explorer to run slowly. Does anyone knows, How to correct that? Thanks.
2
3756
by: John c | last post by:
I have looping code in Access which generates Excel spreadsheets and prints them off. They can run to hundreds and at present, once the code starts to run there is no way it can stop cleanly part way through. For instance, if the wrong printer has been selected. (I know that Ctrl-Break should slam it but doesn't always and anyway that's not too user friendly). So is there any way a user input can work ?
0
9685
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10886
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10634
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10277
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9413
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7817
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7007
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5853
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3126
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.