简体   繁体   English

MS Access 插入 Select 不适用于 vba

[英]MS Access Insert Into Select Does not work with vba

I am trying to run an insert into select query like this:我正在尝试像这样在 select 查询中运行插入:

Dim rs as new ADODB.recordset
rs.Open "INSERT INTO tempTable(PartNumber, Age, Height, Notes, UpdateType)" & _
        " SELECT PartNumber, Age, Height, Notes, UpdateType FROM TABLE2", CurrentProject.Connection, adOpenStatic, adLockOptimistic

which gives me an error:这给了我一个错误:

Syntax Error in INSERT INTO statment INSERT INTO 语句中的语法错误

What is really weird is that when I debug.print the above statement and run the query from the query wizard in Access database it runs just fine.真正奇怪的是,当我 debug.print 上面的语句并从 Access 数据库中的查询向导运行查询时,它运行得很好。

My understanding of currentproject.connection has been that I can happily use the Jet-SQL or MS Access query syntax so if the query runs in the query window in Access it should run in VBA as well.我对currentproject.connection的理解是,我可以愉快地使用 Jet-SQL 或 MS Access 查询语法,所以如果查询在 Access 中的查询 window 中运行,它也应该在 VBA 中运行。 Seems like that is not the case.似乎情况并非如此。

Here are a few other things I have tried to make the code run:以下是我尝试使代码运行的其他一些事情:

CurrentProject.Connection.Execute "INSERT INTO tempTable(PartNumber, Age, Height, Notes, UpdateType)" & _
        " SELECT PartNumber, Age, Height, Notes, UpdateType FROM TABLE2"

This gives the same error as above.这给出了与上面相同的错误。

I also tried to add a new line vbclrf :我还尝试添加一个新行vbclrf

rs.Open "INSERT INTO tempTable(PartNumber, Age, Height, Notes, UpdateType)" & _
        & vbclrf & " SELECT PartNumber, Age, Height, Notes, UpdateType FROM TABLE2", CurrentProject.Connection, adOpenStatic, adLockOptimistic

but it gave me the same error但它给了我同样的错误

Only SELECT statements are used to set a recordset object.只有 SELECT 语句用于设置记录集 object。 A recordset is not needed.不需要记录集。

INSERT is an action SQL so for VBA constructed statement, use DoCmd.RunSQL or CurrentDb.Execute or CurrentProject.Connection.Execute. INSERT 是一个动作 SQL 所以对于 VBA 构造语句,使用 DoCmd.RunSQL 或 CurrentDb.Execute 或 CurrentProject.Connection.Execute。

My preference is: CurrentDb.Execute "your SQL statement here"我的偏好是: CurrentDb.Execute "your SQL statement here"

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

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