提问人:John Samaklak 提问时间:10/25/2023 最后编辑:AndreJohn Samaklak 更新时间:10/26/2023 访问量:56
Access VBA 中的 SQL 查询(我假设语法)问题
SQL Query in Access VBA (I assume syntax) issue
问:
我在 Access 中有一个表,我使用从表单驱动的 SQL 为该表创建了一个动态查询。
Public Sub QueryProductsSPECIFIC()
'General char construction
Wildcart = Chr(34) & "*" & Chr(34)
EndSQL = ";"
'SQL String Construction
StrSQLAA = "SELECT "
StrSQLAB = "ProductList.ID, ProductList.ProductCode, ProductList.ProductType, ProductList.ProductName, ProductList.UOM, ProductList.ProductMainCategory, ProductList.ProductSubCategory, ProductList.SupplierName, ProductList.SupplierProductCode, ProductList.ProductActive, ProductList.SOH, ProductList.CPExclVAT, ProductList.SPExclVAT, ProductList.VATRate, ProductList.VATAmount, ProductList.SPInclVAT, ProductList.MaxDiscount, ProductList.[GP%], ProductList.Image "
StrSQLAC = "FROM "
StrSQLAD = "ProductList "
StrSQLAE = "WHERE "
StrSQLAF = "((ProductList.ProductCode) LIKE " & Wildcart & " & [Forms]![ProductSearch]![SearchProductCode] & " & Wildcart & ")"
StrSQLAG = "((ProductList.ProductName) LIKE " & Wildcart & " & [Forms]![ProductSearch]![SearchProductName] & " & Wildcart & ")"
StrSQL = StrSQLAA & StrSQLAB & StrSQLAC & StrSQLAD & StrSQLAE & StrSQLAF & ", "& StrSQLAG
'Temp line for troubleshooting SQL Code
Debug.Print StrSQL
'Debug.Print strSQLY
'Delete query if it already exists
On Error Resume Next
DoCmd.DeleteObject acQuery, "ProductFullListQuery"
'Create new query
Set qdf = CurrentDb.CreateQueryDef("ProductFullListQuery", StrSQL & EndSQL)
'DoCmd.OpenQuery qdf.Name
'Release
qdf.Close
Set qdf = Nothing
当我排除 StrSQLAG 时,我的查询会运行,当我包含它时,它返回零个结果。我对 SQL 不够了解,不知道我是否在某个地方搞砸了语法。在 StrSQLAF 之后,我也尝试了“, AND” 而不是 “, ”,但我得到了相同的结果。
调试返回:
SELECT ProductList.ID, ProductList.ProductCode, ProductList.ProductType, ProductList.ProductName, ProductList.UOM, ProductList.ProductMainCategory, ProductList.ProductSubCategory, ProductList.SupplierName, ProductList.SupplierProductCode, ProductList.ProductActive, ProductList.SOH, ProductList.CPExclVAT, ProductList.SPExclVAT, ProductList.VATRate, ProductList.VATAmount, ProductList.SPInclVAT, ProductList.MaxDiscount, ProductList.[GP%], ProductList.Image
FROM ProductList
WHERE ((ProductList.ProductCode) LIKE "*" & [Forms]![ProductSearch]![SearchProductCode] & "*"),
((ProductList.ProductName) LIKE "*" & [Forms]![ProductSearch]![SearchProductName] & "*");
以我有限的知识,这对我来说是正确的,但显然不是。
答: 暂无答案
评论
StrSQLAF & " OR " & StrSQLAG
StrSQL = StrSQLAA & StrSQLAB & StrSQLAC & StrSQLAD & StrSQLAE & StrSQLAF & " OR " & StrSQLAG