Access VBA 中的 SQL 查询(我假设语法)问题

SQL Query in Access VBA (I assume syntax) issue

提问人:John Samaklak 提问时间:10/25/2023 最后编辑:AndreJohn Samaklak 更新时间:10/26/2023 访问量:56

问:

我在 Access 中有一个表,我使用从表单驱动的 SQL 为该表创建了一个动态查询。

附上了我的查询表单和基础表。enter image description here

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] & "*");

以我有限的知识,这对我来说是正确的,但显然不是。

SQL VBA MS-ACCESS

评论

2赞 Nick Abbot 10/25/2023
不知道你想得到什么,我会试试这个: 所以整行看起来像:StrSQLAF & " OR " & StrSQLAGStrSQL = StrSQLAA & StrSQLAB & StrSQLAC & StrSQLAD & StrSQLAE & StrSQLAF & " OR " & StrSQLAG
0赞 John Samaklak 10/25/2023
谢谢尼克。是我的逗号搞砸了。我只是用你的OR代替了AND(因为我希望每个字段的查询都根据产品进行评估,以获得所有条件都为真和瞧!它起作用的匹配。非常感谢。

答: 暂无答案