我正在使用Access使用多个(大约18个)条件在数据库中进行搜索。所有这些标准都需要结合起来以提供正确的结果。搜索查询被评估为过于复杂。
在查询中,我使用填写的搜索字段(临时变量)来查找与所有搜索字段匹配的联系人。
该查询如下所示:
SELECT
Contacts.ID,
Contacts.[Naam Contact],
Contacts.Adresgegevens,
Nz([Contacts]![Type Opleiding]) AS [Type opleiding],
Contacts.OplGetal,
Contacts.DataCV,
Nz([Contacts]![Datum Sollicitatie],
DateSerial(1901,1,1)) AS [Datum Sollicitatie],
Contacts.LaatstGewijzigd,
Contacts.Voornaam,
Contacts.Achternaam,
Contacts.Medewerker,
Contacts.Zoektermen,
Contacts.Woonplaats,
Contacts.Geboortejaar,
Contacts.[A],
Contacts.[B],
Contacts.[C],
Contacts.D,
Contacts.E,
Contacts.Overig,
Year(Now())-[Geboortejaar]-1 & "/" & Year(Now())-[Geboortejaar] AS Leeftijd,
Contacts.BinnengekomenBij,
Contacts.Categorie,
Contacts.BinnenBuiten
FROM
Contacts
WHERE
Contacts.[Naam Contact] Like "*" & TempVars!tmpNaam & "*"
And Contacts.Adresgegevens Like "*" & TempVars!tmpAdres & "*"
And Nz(Contacts![Type Opleiding]) Like "*" & TempVars!tmpOpleiding & "*"
And Contacts.OplGetal > TempVars!tmpHGO-1
And Nz(Contacts![Datum Sollicitatie],DateSerial(1901,1,1)) > DateValue(TempVars!tmpDatumSol)
And ( TempVars!tmpBiBu = Contacts.BinnenBuiten
Or TempVars!tmpBiBu = "Maakt niet uit" )
And (TempVars!tmpOP=No Or Contacts.[A]=Yes)
And (TempVars!tmpSP=No Or Contacts.[B]=Yes)
And (TempVars!tmpSS=No Or Contacts.[C]=Yes)
And (TempVars!tmpINS=No Or Contacts.D=Yes)
And (TempVars!tmpE=No Or Contacts.E=Yes)
And (TempVars!tmpOverig=No Or Contacts.Overig=Yes)
And (TempVars!tmpMed=No Or Contacts.Medewerker=Yes)
And (Nz(Contacts.BinnengekomenBij) Like "*" & TempVars!tmpBin & "*"
And (TempVars!tmpCat=Contacts.Categorie Or TempVars!tmpCat="Beide")
如何使该查询更智能,更简单?
提前致谢!
我已经解决了我的问题!复杂性来自于我选中的所有复选框,例如:
(TempVars!tmpE=No Or Contacts.E=Yes)
由于所有这些复选框的组合,查询最终看起来像这样:http : //www.blueclaw-db.com/accessquerysql/complex_query.gif
解决方案是将所有内容都放入VBA代码中,并动态构建查询。现在,只有选中该复选框后,该查询中才会包含该复选框(附加到查询中)。有关复选框E的代码段:
If Me.checkboxE = True Then
strSQL = strSQL + "And Contacts.E=Yes "
End If
这样就消除了查询的复杂性,现在一切都可以顺利运行!
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句