简体   繁体   English

从Access VBA执行SQL Server传递查询

[英]Execute SQL Server Pass-Through Query From Access VBA

I have an UPDATE pass through query saved in Access 2007. When I double-click on the pass through query it runs successfully. 我有一个在Access 2007中保存的UPDATE传递查询。当我双击传递查询时,它成功运行。 How can I get this query to run from VBA? 如何从VBA运行此查询? I'd like it to run when my "splash screen" loads. 当我的“闪屏”加载时,我希望它能够运行。

I'm currently using the following code: 我目前正在使用以下代码:

CurrentDb.Execute "Q_UPDATE_PASSTHROUGH", dbSQLPassThrough

But I get the following message: 但我收到以下消息:

在此输入图像描述

The pass-through query contains all the connection information and I've confirmed the SQL syntax is correct by running it multiple times, so not sure what I'm missing in my VBA call. 传递查询包含所有连接信息,我通过多次运行确认SQL语法是正确的,所以不确定我在VBA调用中缺少什么。

Use the QueryDef's Execute method: 使用QueryDef的Execute方法:

CurrentDb.QueryDefs("Q_UPDATE_PASSTHROUGH").Execute

I don't think you should need to explicitly include the dbSQLPassThrough option here, but you can try like this if you want it: 我认为你不应该在这里显式包含dbSQLPassThrough选项,但如果你需要,可以尝试这样做:

CurrentDb.QueryDefs("Q_UPDATE_PASSTHROUGH").Execute dbSQLPassThrough

I confirm that the QueryDef's Execute method is the recommended way to achieve your goal. 我确认QueryDef的Execute方法是实现目标的推荐方法。

CurrentDb.QueryDefs("Q_UPDATE_PASSTHROUGH").Execute

However, I can point out that in a similar case with Access 2010, using dbSQLPassThrough for the Options parameter caused a Run-time error '3001': Invalid Argument . 但是,我可以指出,在Access 2010的类似情况下,对Options参数使用dbSQLPassThrough导致Run-time error '3001': Invalid Argument

I recently ran into the same problem. 我最近遇到了同样的问题。 While the above mentioned Execute method is working for most cases, some people (me included) experiencing a Run-time error '3001': Invalid Argument when using the parameter dbSQLPassThrough . 虽然上面提到的Execute方法适用于大多数情况,但有些人(包括我)遇到Run-time error '3001': Invalid Argument使用参数dbSQLPassThrough时参数无效 This was also addressed in the answer above me and happens even in the simplest SQL-statements. 这也在我上面的答案中得到解决,即使在最简单的SQL语句中也会发生。

For those who are having the same problem, I recommend using the OpenQuery method as alternative. 对于那些遇到同样问题的人,我建议使用OpenQuery方法作为替代方案。

A valid substitution for the following code 有效替换以下代码

CurrentDb.QueryDefs("Q_UPDATE_PASSTHROUGH").Execute

would be 将会

DoCmd.OpenQuery "Q_UPDATE_PASSTHROUGH"

I know this thread is 4 years old, however, searching for a solution for the not working Execute method on Google brings you directly to this thread which is why I thought it would be useful to add an alternative solution which solved this problem for me. 我知道这个帖子已有4年了,但是,在Google上搜索不起作用的Execute方法的解决方案会直接带你到这个线程,这就是为什么我认为添加替代解决方案为我解决这个问题会很有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM