Insert SQL generated by Access Fails when i try to run it in VBA (1 Viewer)

brucemc777

New member
Local time
Today, 14:30
Joined
Nov 1, 2012
Messages
15
I obtained the following SQL from an Append query in Access:

INSERT INTO tblOrderEquipment ( IDOrderEquipment, IDEquipment, IDOrders, UnitCount )
SELECT tblOrderEquipment.IDOrderEquipment, tblOrderEquipment.IDEquipment, tblOrderEquipment.IDOrders, tblOrderEquipment.UnitCount
FROM tblOrderEquipment
WHERE (((tblOrderEquipment.IDEquipment)=Forms!frmEquipOrder!frmOrderAddEquip.Form!cbxEquipSelect.Column(0)) AND ((tblOrderEquipment.IDOrders)="IDOrders") AND ((tblOrderEquipment.UnitCount)="tbCount"));

and i'm attempting to run it from a command button as follows:

Code:
Private Sub cbAdd_Click()
    Dim SQL As String

    SQL = "INSERT INTO tblOrderEquipment ( IDOrderEquipment, IDEquipment, IDOrders, UnitCount )" _
    & "SELECT tblOrderEquipment.IDOrderEquipment, tblOrderEquipment.IDEquipment," _
    & "tblOrderEquipment.IDOrders, tblOrderEquipment.UnitCount FROM tblOrderEquipment" _
    & "WHERE (((tblOrderEquipment.IDEquipment)=Forms!frmEquipOrder!frmOrderAddEquip.Form!cbxEquipSelect.Column(0))" _
    & "AND ((tblOrderEquipment.IDOrders)=""IDOrders"") AND ((tblOrderEquipment.UnitCount)=""tbCount""));"
    
    DoCmd.RunSQL SQL

End Sub

But i get an error:

Error.png


I also noted when Forms!frmEquipOrder!frmOrderAddEquip.Form!cbxEquipSelect.Column(0) was run in the immediate window i got back the result i am looking for. Apparently there is more to converting an SQL statement from an Append Query than cut & paste-

By looking at the above can anyone tell me what needs to be modified and perhaps how?

Thank you very much for helping me learn and dealing with my confusion!

-Bruce
 

GaP42

Active member
Local time
Tomorrow, 06:30
Joined
Apr 27, 2020
Messages
437
Code:
Private Sub cbAdd_Click()
Dim strSQL As String

strSQL = "INSERT INTO tblOrderEquipment ( IDOrderEquipment, IDEquipment, IDOrders, UnitCount )" _ &
    "SELECT tblOrderEquipment.IDOrderEquipment, tblOrderEquipment.IDEquipment," _ &
        "tblOrderEquipment.IDOrders, tblOrderEquipment.UnitCount FROM tblOrderEquipment" _ &
    "WHERE (((tblOrderEquipment.IDEquipment)=Forms!frmEquipOrder!frmOrderAddEquip.Form!cbxEquipSelect.Column(0))" _ &
    "AND ((tblOrderEquipment.IDOrders)=""IDOrders"") AND ((tblOrderEquipment.UnitCount)=""tbCount""));"

Debug.print strSQL
DoCmd.RunSQL strSQL
Step 1: use debug.print in your code so you can see what the SQL return, comment out this statement after debugging.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 04:30
Joined
May 7, 2009
Messages
19,293
use proper Concatenation on your SQL string:
Code:
Private Sub cbAdd_Click()
    Dim SQL As String

    SQL = "INSERT INTO tblOrderEquipment ( IDOrderEquipment, IDEquipment, IDOrders, UnitCount )" _
    & "SELECT tblOrderEquipment.IDOrderEquipment, tblOrderEquipment.IDEquipment," _
    & "tblOrderEquipment.IDOrders, tblOrderEquipment.UnitCount FROM tblOrderEquipment" _
    & "WHERE (((tblOrderEquipment.IDEquipment)=" & Forms!frmEquipOrder!frmOrderAddEquip.Form!cbxEquipSelect.Column(0))  _
    & " AND ((tblOrderEquipment.IDOrders)='IDOrders') AND ((tblOrderEquipment.UnitCount)='tbCount'));"
    
    DoCmd.RunSQL SQL

End Sub
 

theDBguy

I’m here to help
Staff member
Local time
Today, 13:30
Joined
Oct 29, 2018
Messages
21,700
Hi Bruce. One problem I see is you may be missing a space between the table name and the Where clause.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 15:30
Joined
Feb 28, 2001
Messages
27,683
Darn, too slow, because theDBguy beat me to that one.
 

MarkK

bit cruncher
Local time
Today, 13:30
Joined
Mar 17, 2004
Messages
8,211
I would never rely on such a fragile chain of references as this: Forms!frmEquipOrder!frmOrderAddEquip.Form!cbxEquipSelect.Column(0). If a method consumes data from an object, put the method in that object.
 

plog

Banishment Pending
Local time
Today, 15:30
Joined
May 11, 2011
Messages
11,728
Code:
...ND ((tblOrderEquipment.IDOrders)="IDOrders") AND ((tblOrderEquipment.UnitCount)="tbCount"));

Did you actually run that query from a query object or just build it there? It just doesn't smell right.

1. A value in the field [IDOrders] can have the literal text value of 'IDOrders'? Or are you trying to reference a variable?

2. A field called [UnitCount] is not a numeric field but a text field where the literal text of 'tbCount' is a possible value? Again, are you trying to reference some variable or form field instead?

Also, the spacing issue you have with your FROM that others have pointed out isn't unique to the FROM. Youve made that same spacing error on every new line of your SQL that you put into VBA.
 

isladogs

MVP / VIP
Local time
Today, 21:30
Joined
Jan 14, 2017
Messages
18,330
This is what I get using my SQL <==> VBA converter (originally based on an old utility from Allen Browne):

Code:
SQL = "INSERT INTO tblOrderEquipment ( IDOrderEquipment, IDEquipment, IDOrders, UnitCount )" & _
" SELECT tblOrderEquipment.IDOrderEquipment, tblOrderEquipment.IDEquipment, tblOrderEquipment.IDOrders, tblOrderEquipment.UnitCount" & _
" FROM tblOrderEquipment" & _
" WHERE (((tblOrderEquipment.IDEquipment)=Forms!frmEquipOrder!frmOrderAddEquip.Form!cbxEquipSelect.Column(0))" & _
" AND ((tblOrderEquipment.IDOrders)=""IDOrders"") AND ((tblOrderEquipment.UnitCount)=""tbCount""));"

However, as already stated in posts #6 & #7, the where filter items all need attention


I've just updated this utility to v3.27 today
 
Last edited:

Gasman

Enthusiastic Amateur
Local time
Today, 21:30
Joined
Sep 21, 2011
Messages
14,747
I obtained the following SQL from an Append query in Access:

INSERT INTO tblOrderEquipment ( IDOrderEquipment, IDEquipment, IDOrders, UnitCount )
SELECT tblOrderEquipment.IDOrderEquipment, tblOrderEquipment.IDEquipment, tblOrderEquipment.IDOrders, tblOrderEquipment.UnitCount
FROM tblOrderEquipment
WHERE (((tblOrderEquipment.IDEquipment)=Forms!frmEquipOrder!frmOrderAddEquip.Form!cbxEquipSelect.Column(0)) AND ((tblOrderEquipment.IDOrders)="IDOrders") AND ((tblOrderEquipment.UnitCount)="tbCount"));

and i'm attempting to run it from a command button as follows:

Code:
Private Sub cbAdd_Click()
    Dim SQL As String

    SQL = "INSERT INTO tblOrderEquipment ( IDOrderEquipment, IDEquipment, IDOrders, UnitCount )" _
    & "SELECT tblOrderEquipment.IDOrderEquipment, tblOrderEquipment.IDEquipment," _
    & "tblOrderEquipment.IDOrders, tblOrderEquipment.UnitCount FROM tblOrderEquipment" _
    & "WHERE (((tblOrderEquipment.IDEquipment)=Forms!frmEquipOrder!frmOrderAddEquip.Form!cbxEquipSelect.Column(0))" _
    & "AND ((tblOrderEquipment.IDOrders)=""IDOrders"") AND ((tblOrderEquipment.UnitCount)=""tbCount""));"
  
    DoCmd.RunSQL SQL

End Sub

But i get an error:

View attachment 114630

I also noted when Forms!frmEquipOrder!frmOrderAddEquip.Form!cbxEquipSelect.Column(0) was run in the immediate window i got back the result i am looking for. Apparently there is more to converting an SQL statement from an Append Query than cut & paste-

By looking at the above can anyone tell me what needs to be modified and perhaps how?

Thank you very much for helping me learn and dealing with my confusion!

-Bruce
Tip: Put a space in front of each new line after the " . Much easier to ensure a space is there, rather than scrolling all the way over to thr right all the time. :(
Test the Select code before adding the Insert code.
Also add a Debug.Print SQL before ever trying to use it. Then comment out/remove when it is correct.
 

Users who are viewing this thread

Top Bottom