VBA 查询未像访问查询那样运行
Posted
技术标签:
【中文标题】VBA 查询未像访问查询那样运行【英文标题】:VBA Query Not Running as Access Query Does 【发布时间】:2012-08-08 15:24:01 【问题描述】:对于您即将收到的代码集群,我深表歉意。问题是:为什么我的查询在设计窗口中工作(注意Debug.Print .CommandText
输出我放入访问查询的确切文本),但提出了 Method 'Execute' of object '_Command' failed 并且不运行 INSERT 查询。代码如下:
cnn.BeginTrans 'Begin deletion
cmdText = "DELETE * FROM tblB1B2"
Set cmd = New ADODB.Command
With cmd
.CommandType = adCmdText
.ActiveConnection = cnn
.CommandText = cmdText
.Execute
End With
cnn.CommitTrans 'Commit Deletion
cnn.BeginTrans 'Begin update
cmdText = "INSERT INTO tblB1B2 IN '" & toDB & "' " & _
"SELECT interne AS interne, groupe AS groupe, MAX(itemref) AS [item], largeur AS largeur, hauteur AS hauteur, local AS [local], etage AS etage, partie AS partie, dessin AS dessin, datezonage AS datezonage, externe AS externe, erreur AS erreur, tag_hndl AS tag_hndl, tag_dessin AS tag_dessin, pobj_hndl AS pobj_hndl, pobj_type AS pobj_type, pobj_bname AS pobj_bname, zpl_hndl AS zpl_hndl, id AS id, Sidewalk AS Sidewalk, BarrierFreePathOfTravel AS BarrierFreePathOfTravel, Signage AS Signage, Hardware AS Hardware, PowerDoorOperator AS PowerDoorOperator, PDOMountHeight AS PDOMountHeight, ClearWidth AS ClearWidth, ClearSpace AS ClearSpace, ManoeuveringSpace AS ManoeuveringSpace, TwoDoorsInASeries AS TwoDoorsInASeries, Comments AS [Comments] " & _
"FROM " & _
"(SELECT interne, groupe, [item] AS itemref, largeur, hauteur, local, etage, partie, dessin, datezonage, externe, erreur, tag_hndl, tag_dessin, pobj_hndl, pobj_type, pobj_bname, zpl_hndl, [id], Sidewalk, BarrierFreePathOfTravel, Signage, Hardware, PowerDoorOperator, PDOMountHeight, ClearWidth, ClearSpace, ManoeuveringSpace, TwoDoorsInASeries, [Comments] " & _
"FROM tblB1B2 IN '" & dbArray(0) & "' " & _
"UNION ALL " & _
"SELECT interne, groupe, [item] AS itemref, largeur, hauteur, local, etage, partie, dessin, datezonage, externe, erreur, tag_hndl, tag_dessin, pobj_hndl, pobj_type, pobj_bname, zpl_hndl, [id], Sidewalk, BarrierFreePathOfTravel, Signage, Hardware, PowerDoorOperator, PDOMountHeight, ClearWidth, ClearSpace, ManoeuveringSpace, TwoDoorsInASeries, [Comments] " & _
"FROM tblB1B2 IN '" & dbArray(1) & "' " & _
"UNION ALL " & _
"SELECT interne, groupe, [item] AS itemref, largeur, hauteur, local, etage, partie, dessin, datezonage, externe, erreur, tag_hndl, tag_dessin, pobj_hndl, pobj_type, pobj_bname, zpl_hndl, [id], Sidewalk, BarrierFreePathOfTravel, Signage, Hardware, PowerDoorOperator, PDOMountHeight, ClearWidth, ClearSpace, ManoeuveringSpace, TwoDoorsInASeries, [Comments] " & _
"FROM tblB1B2 IN '" & dbArray(2) & "' " & _
") AS FullTable " & _
"GROUP BY interne, groupe, largeur, hauteur, local, etage, partie, dessin, datezonage, externe, erreur, tag_hndl, tag_dessin, pobj_hndl, pobj_type, pobj_bname, zpl_hndl, [id], Sidewalk, BarrierFreePathOfTravel, Signage, Hardware, PowerDoorOperator, PDOMountHeight, ClearWidth, ClearSpace, ManoeuveringSpace, TwoDoorsInASeries, [Comments]"
Set cmd = New ADODB.Command
With cmd
.CommandType = adCmdText
.ActiveConnection = cnn
.CommandText = cmdText
Debug.Print .CommandText
.Execute
End With
cnn.CommitTrans 'Commit update
复制粘贴到访问查询中的打印文本运行良好;但在 VBA 中会产生此错误。为什么?我怎样才能绕过它?我是否使用了我不知道的关键字?
另外,有没有一种方法可以在不指定每个字段的情况下执行此查询? (注意MAX(itemref)
)
【问题讨论】:
【参考方案1】:嗯.. 在这里发布后(像往常一样,经过大约 3 小时的试验和失败).. 我明白了。原来“local”是 JET 中的保留关键字。我必须做一些创造性的操作才能让它认识到它是一个领域。对于遇到此类问题的其他任何人,这是固定的 .CommandText:
cmdText = "INSERT INTO tblB1B2 IN '" & toDB & "' " & _
"SELECT interne AS interne, groupe AS groupe, MAX(itemref) AS item, largeur AS largeur, hauteur AS hauteur, localref AS [local], etage AS etage, partie AS partie, dessin AS dessin, datezonage AS datezonage, externe AS externe, erreur AS erreur, tag_hndl AS tag_hndl, tag_dessin AS tag_dessin, pobj_hndl AS pobj_hndl, pobj_type AS pobj_type, pobj_bname AS pobj_bname, zpl_hndl AS zpl_hndl, id AS id, Sidewalk AS Sidewalk, BarrierFreePathOfTravel AS BarrierFreePathOfTravel, Signage AS Signage, Hardware AS Hardware, PowerDoorOperator AS PowerDoorOperator, PDOMountHeight AS PDOMountHeight, ClearWidth AS ClearWidth, ClearSpace AS ClearSpace, ManoeuveringSpace AS ManoeuveringSpace, TwoDoorsInASeries AS TwoDoorsInASeries, Comments AS Comments " & _
"FROM " & _
"(SELECT interne, groupe, item AS itemref, largeur, hauteur, [local] AS localref, etage, partie, dessin, datezonage, externe, erreur, tag_hndl, tag_dessin, pobj_hndl, pobj_type, pobj_bname, zpl_hndl, id, Sidewalk, BarrierFreePathOfTravel, Signage, Hardware, PowerDoorOperator, PDOMountHeight, ClearWidth, ClearSpace, ManoeuveringSpace, TwoDoorsInASeries, Comments " & _
"FROM tblB1B2 IN '" & dbArray(0) & "' " & _
"UNION ALL " & _
"SELECT interne, groupe, item AS itemref, largeur, hauteur, [local] AS localref, etage, partie, dessin, datezonage, externe, erreur, tag_hndl, tag_dessin, pobj_hndl, pobj_type, pobj_bname, zpl_hndl, [id], Sidewalk, BarrierFreePathOfTravel, Signage, Hardware, PowerDoorOperator, PDOMountHeight, ClearWidth, ClearSpace, ManoeuveringSpace, TwoDoorsInASeries, Comments " & _
"FROM tblB1B2 IN '" & dbArray(1) & "' " & _
"UNION ALL " & _
"SELECT interne, groupe, item AS itemref, largeur, hauteur, [local] AS localref, etage, partie, dessin, datezonage, externe, erreur, tag_hndl, tag_dessin, pobj_hndl, pobj_type, pobj_bname, zpl_hndl, [id], Sidewalk, BarrierFreePathOfTravel, Signage, Hardware, PowerDoorOperator, PDOMountHeight, ClearWidth, ClearSpace, ManoeuveringSpace, TwoDoorsInASeries, Comments " & _
"FROM tblB1B2 IN '" & dbArray(2) & "' " & _
") AS FullTable " & _
"GROUP BY interne, groupe, largeur, hauteur, localref, etage, partie, dessin, datezonage, externe, erreur, tag_hndl, tag_dessin, pobj_hndl, pobj_type, pobj_bname, zpl_hndl, [id], Sidewalk, BarrierFreePathOfTravel, Signage, Hardware, PowerDoorOperator, PDOMountHeight, ClearWidth, ClearSpace, ManoeuveringSpace, TwoDoorsInASeries, Comments"
注意local
字段,我必须在子查询中使用[local] AS localref
,在主查询中使用localref AS [local]
,以避免关键字和循环引用。
【讨论】:
干得好,保留字不需要别名,方括号应该可以解决问题。 是的,但由于某种原因,它导致了与 [local] AS [local] 的循环引用,但没有与任何其他字段 AS 字段引用..不知道为什么。以上是关于VBA 查询未像访问查询那样运行的主要内容,如果未能解决你的问题,请参考以下文章