如何停止运行子程序? [英] How to stop running sub routine?
问题描述
我有一个子例程,可以在Outlook PST中搜索发送给某个特定电子邮件地址的消息
。显然,当PST包含数千条消息时,这可能需要很长时间
。我想在我的
Access 20003表单上放一个按钮,该表单将在单击时停止子例程。我知道我可以
按Ctl + Break并停止代码,但有更优雅的方式以编程方式执行这个吗?我应该使用SendKeys吗?
此外,如果有数据表(显示
电子邮件搜索的结果)填充找到的消息会很好找到它们的标题 -
我还在考虑如何做到这一点......任何想法/建议都是非常受欢迎的
非常欢迎!!
谢谢!
On Sun,2004年1月11日07:34:20 GMT in comp.databases.ms-访问,deko
< dj **** @ hotmail.com>写道:
我有一个子例程,在Outlook PST中搜索发送到特定电子邮件地址的消息。显然,当PST包含数千条消息时,这可能需要很长时间。我想在我的
Access 20003表单上放一个按钮,该按钮将在单击时停止子例程。我知道我可以按下Ctl + Break并停止代码,但是有更优雅的方式来编程吗?我应该使用SendKeys吗?
此外,如果发现找到的邮件标题,那么将数据表(显示电子邮件搜索的结果)填充到最好是很好的。 />我还在考虑如何做到这一点....任何想法/建议都非常欢迎!!
20003嗯?哇,不能等待那个< g>
我假设你的代码有某种循环?例如
同时< somecondition>
..做消息
循环
如果这是在一个表单中,那么创建一个模块级变量(在
decations部分,例如
Dim mfSearching as Boolean,并且有一个停止该表单上的按钮
或者如果在公共模块中然后是公共或全局变量,则使用带有停止的
弹出窗体。停止按钮'的代码将
看起来像......
Sub cmdStop_Click()
mfSearching = False
结束子
现在回到你的主循环,它应该做一些事情
of ...
mfSearching = True
做一会儿(< somecondition>)和mfSearching
..做消息
Doevents
循环
当
按下模块时,你需要DoEvents来按下停止按钮/全局变量m fSearch设置为false,
主循环将自然结束。
我经常做类似的事情并缩小范围重新使用开始按钮所需的
变量,例如
Sub cmdStart_Click()
静态fRunning As Boolean''注意必须是静态
如果fRunning那么
fRunning = False
cmdStart.Caption ="停止>
DoEvents
退出子
结束如果
fRunning = True
cmdStart.Caption ="停止>
同时< somecondition>并且fRunning
...做点什么
DoEvents
循环
cmdStart.Caption ="开始"
结束子
注意如果使用For循环,则在循环中插入一些代码,如:
if不是mfRunning那么
退出
结束如果
同样可以用于do循环(使用退出做)以防万一。每次迭代都要做很多事情。
-
A)bort ,R)etry,I)用大锤子影响。
>注意如果使用For循环,则在循环中插入一些代码,如:
如果不是mfRunning那么
退出
结束如果
这是For Each ... Next循环。
所以...要添加的代码是:
Dim mfRunning as Boolean
然后在For Each循环中:
如果不是mfRunning那么
退出
结束如果
和cmdStart_Click按钮,如下所示:
Sub cmdStart_Click()
静态fRunning为布尔值
如果fRunning那么
fRunning = False
cmdStart.Caption ="停止>
DoEvents
如果
fRunning = True
cmdStart.Caption ="停止>
$ b $ ..
...做点什么
DoEvents
下一页
cmdStart.Caption ="开始"
结束子
我将不得不玩这个...谢谢你指点我
正确的方向...
在表单的代码顶部,添加
dim blnStop as boolean
开始搜索之前
blnStop = false
STOP按钮的'click'事件
blnStop = true
在您的搜索循环中
如果(blnstop)则
退出子
结束如果
.....
创建一个名为tblMessagesFound的表,其中包含您想要的字段
在您的数据表中查看
运行搜索之前
delete * from tblMessagesFound
while搜索,向表中添加新消息
dim rs as recordset
set rs = currentdb.openrecordset(" tblMessagesFound")
你的循环
如果发现了什么
rs.addNew
rs!message = ......
rs.update
结束如果
你的循环结束
rs.close
set rs = nothing
使用tblMessagesFound作为源创建数据表表格
" deko" < DJ **** @ hotmail.com>在消息新闻中写道:< 00 ***************** @ newssvr25.news.prodigy.c om> ...我有一个子例如,在Outlook PST中搜索发送到特定电子邮件地址的消息。显然,当PST包含数千条消息时,这可能需要很长时间。我想在我的
Access 20003表单上放一个按钮,该按钮将在单击时停止子例程。我知道我可以按下Ctl + Break并停止代码,但是有更优雅的方式来编程吗?我应该使用SendKeys吗?
此外,如果发现找到的邮件标题,那么将数据表(显示电子邮件搜索的结果)填充到最好是很好的。 />我还在考虑如何做到这一点......任何想法/建议都非常欢迎!!
谢谢!
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 programmatically? 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!
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 programmatically? 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 <somecondition>
.. 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.Caption="Stopping"
DoEvents
exit sub
end if
fRunning = True
cmdStart.Caption="Stop"
Do while <somecondition> And fRunning
... do something
DoEvents
Loop
cmdStart.Caption="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.
> 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.Caption="Stopping"
DoEvents
exit sub
end if
fRunning = True
cmdStart.Caption="Stop"
For Each ...
... do something
DoEvents
Next
cmdStart.Caption="Start"
End Sub
I''ll have to play around with this a bit... thanks for pointing me in the
right direction...
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 tblMessagesFound with the fields that you want
to see in your datasheet
before running the search
delete * from tblMessagesFound
while search, add new messages to the table
dim rs as recordset
set rs = currentdb.openrecordset("tblMessagesFound")
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 tblMessagesFound as the source
"deko" <dj****@hotmail.com> wrote in message news:<00*****************@newssvr25.news.prodigy.c om>...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 programmatically? 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!
这篇关于如何停止运行子程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!