StuckAtWork
StuckAtWork

Reputation: 1633

VBA Query Not Running as Access Query Does

I apologize for the cluster of code you're about to receive. The question is: why does my query work in the design window (note the Debug.Print .CommandText outputs the exact text I put into an access query), but comes up with Method 'Execute' of object '_Command' failed and does not run the INSERT query. Here's the code:

    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

The printed text when copy-pasted into an access query runs fine; but produces this error in VBA. Why? How can I get around it? Am I using a keyword I'm not aware of?

Also, is there a way I can perform this query without specifying EACH field? (Note the MAX(itemref))

Upvotes: 2

Views: 500

Answers (1)

StuckAtWork
StuckAtWork

Reputation: 1633

Well.. after posting here (as usual, after about 3 hours of trial and failure).. I got it. Turns out "local" is a reserved keyword in JET. I had to do some creative manoeuvring to get it to recognize that it's a field. For anyone else having this type of problem, here's the fixed .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"

Note around the local fields, I had to have [local] AS localref in the subqueries and localref AS [local] in the main query to avoid both keywords and circular referencing.

Upvotes: 1

Related Questions