简体   繁体   中英

Where with And Clause SQL, Access-VBA

运行时错误消息

Hi Everyone, Another Installment of WTF am I missing? When I run this code I get a missing operator run-time Error, see attached photo.

I can't seem to figure it out. This string is used to open a recordset that will populate a form.

Private Sub BtnUseSelection_Click()

Dim CompSQL As String

CompSQL = "SELECT T1Company.LegalName, T2AddressType.AddressType, T1Addresses.City" & _
                    " FROM T2AddressType INNER JOIN (T1Addresses INNER JOIN (T1Company INNER JOIN T3Company_Addresses ON T1Company.CompanyID = T3Company_Addresses.CompanyID) ON T1Addresses.AddressID = T3Company_Addresses.AddressID)" & _
                    " ON T2AddressType.AddressTypeID = T1Addresses.AddressType" & _
                    " WHERE T1Company.LegalName = " & Me.LstboxCompanies.Column(0) & " And T2AddressType.AddressType = " & Me.LstboxCompanies.Column(1) & " And T1Addresses.City = " & Me.LstboxCompanies.Column(2)

Set db = CurrentDb

Set RSCompany = db.OpenRecordset(CompSQL, dbOpenSnapshot, dbSeeChanges)

Not quite sure what I am missing, any help would be greatly appreciated.

Consider parameterizing query to avoid any need for quote enclosure or string concatenation.

SQL (save below as a saved Access query, more efficient than VBA string query as database engine saves best execution plan)

PARAMETERS [LegalNameParam] Text(255), [AddressTypeParam] Text(255),
           [CityParam] Text(255);
SELECT T1Company.LegalName, T2AddressType.AddressType, T1Addresses.City
FROM T2AddressType
INNER JOIN (T1Addresses INNER JOIN (T1Company
            INNER JOIN T3Company_Addresses
             ON T1Company.CompanyID = T3Company_Addresses.CompanyID)
  ON T1Addresses.AddressID = T3Company_Addresses.AddressID)
  ON T2AddressType.AddressTypeID = T1Addresses.AddressType
WHERE T1Company.LegalName = [LegalNameParam]
 AND T2AddressType.AddressType = [AddressTypeParam]
 AND T1Addresses.City = [CityParam]

VBA (call the above query and bind values to named parameters)

Dim db As Database, RSCompany As Recordset, qdef As QueryDef
Dim CompSQL As String

Set db = CurrentDb    
Set qdef = db.QueryDefs("myStoredQuery")

qdef!LegalNameParam = Me.LstboxCompanies.Column(0)
qdef!AddressTypeParam = Me.LstboxCompanies.Column(1)
qdef!CityParam = Me.LstboxCompanies.Column(2)

Set RSCompany = qdef.OpenRecordset(dbOpenSnapshot, dbSeeChanges)

It seems you've missed the ' ' around your string literals.

instead of

... WHERE T1Company.LegalName = " & Me.LstboxCompanies.Column(0) & " ...

use:

... WHERE T1Company.LegalName = '" & Me.LstboxCompanies.Column(0) & "' ...

You also need to escape your strings to avoid corruption of the command (or sql injections). Look here and here for some info.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM