2

对于您即将收到的代码集群,我深表歉意。问题是:为什么我的查询在设计窗口中工作(注意Debug.Print .CommandText输出我放入访问查询的确切文本),但是对象'_Command'的方法'Execute'失败并且不运行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)

4

1 回答 1

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]主查询中使用,以避免关键字和循环引用。

于 2012-08-08T15:55:54.713 回答