vba Access 2013 中的 SendKeys 似乎不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23837855/
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
SendKeys in Access 2013 does not seem to be working
提问by user3670204
I am new to vba and macros in Access and this is my first time posting a question online. I am using Access 2013. I have searched a variety of online resources for my answer. I thought the SendKeys command was the answer, but it is not working for me. I'm not sure what I am doing wrong. Below outlines what I am trying to accomplish and the steps of the code that aren't working as I intended. Any suggestions would be appreciated. Thank you.
我是 Access 中的 vba 和宏的新手,这是我第一次在线发布问题。我正在使用 Access 2013。我已经搜索了各种在线资源以获得我的答案。我认为 SendKeys 命令是答案,但它对我不起作用。我不确定我做错了什么。下面概述了我要完成的工作以及未按预期工作的代码步骤。任何建议,将不胜感激。谢谢你。
Purpose: Once per quarter, my client will receive an updated access database. All table names should be identical each quarter. I want them to be able to simply run a macro, specify the location of the new file, and then the macro updates the linked tables and executes all other queries I've built (I have the last part working). So, the part I have not be able to get working is to check the “Always prompt for a new location box”, check the “select all” box and click OK (and then click OK and close after the client specifies the new file location). Below is the code I am using.
目的:每季度一次,我的客户将收到更新的访问数据库。每个季度的所有表名都应该相同。我希望他们能够简单地运行一个宏,指定新文件的位置,然后宏更新链接表并执行我构建的所有其他查询(我有最后一部分工作)。因此,我无法开始工作的部分是选中“始终提示输入新位置框”,选中“全选”框并单击“确定”(然后在客户端指定新文件后单击“确定”并关闭)地点)。下面是我正在使用的代码。
Function Open_LinkedTableManager() DoCmd.RunCommand acCmdLinkedTableManager 'this step works fine 'the following lines, up until Application.Run don't appear to be 'doing anything. The code will run, but I have to manually execute 'each of the steps I am trying to automate before it gets to the 'Application.Run step SendKeys "%a", True ' also tried SendKeys "%(a)" and "+a", "a", etc, 'True; Alt+a checks the "Always prompt for a new location box” SendKeys "%s", True ' also tried SendKeys "%(s)", True; Alt+s checks the "select all" 'box SendKeys "{Enter}" ' then user specifies location of new file SendKeys "{Enter}" ' click OK after receiving message "All selected linked tables 'were successfully refreshed" ' click Close to close linked table manager and proceed to the next step below (not 'sure how to do this) Application.Run ("Update_all_queries") ' this is working; End Sub
Function Open_LinkedTableManager() DoCmd.RunCommand acCmdLinkedTableManager 'this step works fine 'the following lines, up until Application.Run don't appear to be 'doing anything. The code will run, but I have to manually execute 'each of the steps I am trying to automate before it gets to the 'Application.Run step SendKeys "%a", True ' also tried SendKeys "%(a)" and "+a", "a", etc, 'True; Alt+a checks the "Always prompt for a new location box” SendKeys "%s", True ' also tried SendKeys "%(s)", True; Alt+s checks the "select all" 'box SendKeys "{Enter}" ' then user specifies location of new file SendKeys "{Enter}" ' click OK after receiving message "All selected linked tables 'were successfully refreshed" ' click Close to close linked table manager and proceed to the next step below (not 'sure how to do this) Application.Run ("Update_all_queries") ' this is working; End Sub
回答by user3654095
If sending to yourself then try DoEvents after each sendkey.
如果发送给自己,则在每个 sendkey 之后尝试 DoEvents。
DoEvents Function
DoEvents 函数
Yields execution so that the operating system can process other events.
产生执行,以便操作系统可以处理其他事件。
Syntax
句法
DoEvents( )
Remarks
评论
The DoEvents function returns an Integer representing the number of open forms in stand-alone versions of Visual Basic, such as Visual Basic, Professional Edition. DoEvents returns zero in all other applications.
DoEvents 函数返回一个整数,表示在 Visual Basic 的独立版本(如 Visual Basic 专业版)中打开的窗体数。DoEvents 在所有其他应用程序中返回零。
DoEvents passes control to the operating system. Control is returned after the operating system has finished processing the events in its queue and all keys in the SendKeys queue have been sent.
DoEvents 将控制权传递给操作系统。在操作系统完成处理其队列中的事件并且 SendKeys 队列中的所有密钥都已发送后,控制权返回。
DoEvents is most useful for simple things like allowing a user to cancel a process after it has started, for example a search for a file. For long-running processes, yielding the processor is better accomplished by using a Timer or delegating the task to an ActiveX EXE component.. In the latter case, the task can continue completely independent of your application, and the operating system takes case of multitasking and time slicing.
DoEvents 对于简单的事情最有用,比如允许用户在进程开始后取消它,例如搜索文件。对于长时间运行的进程,通过使用 Timer 或将任务委托给 ActiveX EXE 组件来更好地完成处理器。在后一种情况下,任务可以继续完全独立于您的应用程序,并且操作系统采用多任务处理的情况和时间切片。
Caution Any time you temporarily yield the processor within an event procedure, make sure the procedure is not executed again from a different part of your code before the first call returns; this could cause unpredictable results. In addition, do not use DoEvents if other applications could possibly interact with your procedure in unforeseen ways during the time you have yielded control.
注意 任何时候在事件过程中临时让出处理器时,请确保在第一次调用返回之前不会从代码的不同部分再次执行该过程;这可能会导致不可预测的结果。此外,如果其他应用程序可能在您获得控制权期间以不可预见的方式与您的过程交互,请不要使用 DoEvents。
回答by Adam
I have solved your dilemma. All I needed to do was place the sendkey statements before the call to to the linked tabled manager. See Below - Worked Great For Me! I was also able to add all of the commands in your order and they worked great. Good luck, hope this helped. Let me know. Adam
我已经解决了你的困境。我需要做的就是在调用链接表管理器之前放置 sendkey 语句。见下文 - 对我很有用!我还能够按您的顺序添加所有命令,并且它们运行良好。祝你好运,希望这有帮助。让我知道。亚当
PS: If you have many tables to change the path on, this will be painful for the user for every table you are forcing them to have to set the path for.
PS:如果您有许多表要更改路径,那么对于您迫使他们必须为其设置路径的每个表,这对用户来说将是痛苦的。
SendKeys "%s", 1
DoCmd.RunCommand acCmdLinkedTableManager