VBA查询未作为访问查询运行

时间:2012-08-08 15:24:02

标签: vba ms-access jet

我为您即将收到的代码群道歉。问题是:为什么我的查询在设计窗口中起作用(注意Debug.Print .CommandText输出我在访问查询中输入的确切文本),但是出现方法'执行'对象'_Command'失败并且不运行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中产生此错误。为什么?我怎么能绕过它呢?我使用的是我不知道的关键字吗?

另外,有没有办法在不指定EACH字段的情况下执行此查询? (注意MAX(itemref)

1 个答案:

答案 0 :(得分: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],以避免关键字和循环引用。