vba MS Access 中的多线程,异步处理

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2898953/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 11:39:49  来源:igfitidea点击:

multi-thread in MS Access, async processing

multithreadingms-accessvba

提问by Icode4food

I know that title sounds crazy but here is my situation.

我知道这个标题听起来很疯狂,但这是我的情况。

After a certain user event I need to update a couple tables that are "unrelated" to what the user is currently doing. Currently this takes a couple seconds to execute and causes the user a certain amount of frustration. Is there a way to perform my update in a second process or in a manner that doesn't "freeze" the UI of my app while it is processing?

在某个用户事件之后,我需要更新几个与用户当前正在做的事情“无关”的表。目前,这需要几秒钟的时间来执行并导致用户一定程度的挫败感。有没有办法在第二个进程中执行我的更新,或者在处理时不会“冻结”我的应用程序的 UI?

Thanks

谢谢

采纳答案by Raj More

I would work on the heart of the problem - Tune the data update queries to run faster.

我会解决问题的核心 - 调整数据更新查询以使其运行得更快。

Having said that, MS Access does not support multi-threading.

话虽如此,MS Access 不支持多线程。

So, when you make a blocking call to a procedure, MS Access will freeze the screen until the call returns.

因此,当您对过程进行阻塞调用时,MS Access 将冻结屏幕,直到调用返回。

edit

编辑

DAO isn't really your best friend if you are updating a large table over a slow network connection. You might want to consider switching to using an ODBC connection and running a optimizedupdate statement.

如果您通过慢速网络连接更新大表,DAO 并不是您最好的朋友。您可能需要考虑切换到使用 ODBC 连接并运行优化的更新语句。

edit 2

编辑 2

when you use ODBC, you have to write ADO style code to make this work. Note this sample this code is OTTOMH.

当您使用 ODBC 时,您必须编写 ADO 风格的代码来完成这项工作。请注意此示例代码是 OTTOMH。

dim myConn as ADODB.Connection
dim myCmd as ADODB.Command

set myConn = new ADODB.Connection
myConn.ConnectionString = "Provider=SQLOLEDB;Server=MyServerName;Initial Catalog=MyCatalogName;UID='XXX';PWD='YYY'"
myConn.Open

set myCmd =  new ADODB.Command (myConn)
myCmd.SQL = "Update MyTable Set MyColumn = '" & MyDataVariable & "' Where MyPK = '" & MyPKVariable & "'"
myCmd.Execute

myCmd.close
myConn.close

回答by PowerUser

Does the client need confirmation that the info was updated? If not, then you could open a shell routine which handles the update for you. i.e.

客户是否需要确认信息已更新?如果没有,那么您可以打开一个 shell 例程来为您处理更新。IE

Shell("'C:\Reports\SomeOtherAccessDB.MDB' /x 'SomeMacro'", 1)

Shell("'C:\Reports\SomeOtherAccessDB.MDB' /x 'SomeMacro'", 1)

By default, this is asynchronous, so all the user will see is a second .mdb on the taskbar for the few seconds that it takes to run.

默认情况下,这是异步的,因此用户将看到的只是任务栏上的第二个 .mdb 文件,用于运行所需的几秒钟。

EDIT

编辑

Oh, and Kudos for actually caring about your user's time!

哦,感谢您真正关心用户的时间!

回答by Tony Toews

Using docmd.hourglass true before your update statement and docmd.hourglass false after to change the curasor back. Also be sure to put docmd.hourglass false in your error handling routine.

在更新语句之前使用 docmd.hourglass true ,在将光标更改回来之后使用 docmd.hourglass false 。还要确保将 docmd.hourglass false 放在您的错误处理例程中。

Not the fanciest solution but it might help with the user expectation level.

不是最好的解决方案,但它可能有助于用户的期望水平。