大家好,我错过了WTF的另一部分? 当我运行此代码时,我得到一个缺少的运算符运行时错误,请参阅附件照片。
我似乎无法弄明白。此字符串用于打开将填充表单的记录集。
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)
不太确定我错过了什么,非常感谢任何帮助。
答案 0 :(得分:2)
考虑parameterizing query以避免需要引用封装或字符串连接。
SQL (保存下面作为已保存的Access查询,比VBA字符串查询更有效,因为数据库引擎可以保存最佳执行计划)
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 (调用上述查询并将值绑定到命名参数)
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)
答案 1 :(得分:0)