简体   繁体   中英

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. How can I get this query to run from 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.

Use the QueryDef's Execute method:

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:

CurrentDb.QueryDefs("Q_UPDATE_PASSTHROUGH").Execute dbSQLPassThrough

I confirm that the QueryDef's Execute method is the recommended way to achieve your goal.

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 .

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 . This was also addressed in the answer above me and happens even in the simplest SQL-statements.

For those who are having the same problem, I recommend using the OpenQuery method as alternative.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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