SQL syntax error (VBA issue)

pook_666

Board Regular
Joined
Aug 16, 2018
Messages
94
Hi team - I am getting a "incorrect syntax near 'cust_id' error from the below code in VBA.

I've tried the below in SQL Server and it works, so must be an issue in VBA?

Not sure if this is more an SQL query rather than VBA issue, but any help would be great! Thanks!!

VBA Code:
SQLQry = "SELECT cust_name, cust_contact" & _
    "FROM customers" & _
    "WHERE cust_id IN (SELECT cust_id" & _
                      "FROM orders" & _
                      "WHERE order_num IN (SELECT order_num" & _
                                          "FROM orderitems" & _
                                          "WHERE prod_id = 'RGAN01'))"
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Try the following by either putting a space before each closing double quote on every line or one after every opening one
try a debut.print or message sqlqry and you will see it all joined up with no space between each key word
so you might see cust_contactFROM instead of cust_contact FROM once you apply the space
 
Upvote 0
Just as simple as that....how annoying!! Thanks a lot Jim. Always forget about the debut.print.....need to have that hard-coded into my brain!
 
Upvote 0
Grrr auto correct should have said debug.print
Loads of people forget about the space including me
 
Upvote 0

Forum statistics

Threads
1,214,825
Messages
6,121,788
Members
449,049
Latest member
greyangel23

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top