Robert Criqui
Robert Criqui

Reputation: 218

Why do I need brackets around this word to run an SQL query in Excel VBA?

I have a query I'm running in excel vba. I'm trying to open up a recordset using the ADO library. I ran this code in access vba and it worked fine. When I tried doing it in excel VBA it failed to open up the recordset. It finally worked once I put brackets around the vz.Zone portion, but the brackets were not needed anywhere else. I'm trying to figure out why this is needed here. I've seen around that the brackets are needed for any columns that have spaces which this does not, or for reserved keywords which this does not appear to be. Can anyone shed some insight on this issue? I appreciate the help.

Cost = "SELECT vz.vendorID, [vz.Zone], cl.CountryName, V.VendorName" & _
" FROM ((VendorZones AS VZ INNER JOIN CountryList AS CL" & _
" ON VZ.CountryID = CL.CountryID) INNER JOIN Vendors AS V ON" & _
" VZ.VendorID = V.VendorID) WHERE CL.CountryName = 'Austria'"

Upvotes: 1

Views: 282

Answers (1)

Nathan_Sav
Nathan_Sav

Reputation: 8531

Zone is a reserved word so you need to bracket it. https://msdn.microsoft.com/en-us/library/ms189822.aspx

Upvotes: 5

Related Questions