admin

MS Access:筛选器查询组合框错误

sql

我的问题:我有一个查询,该查询根据在表单中的复选框,文本框和组合框中输入的内容来过滤数据。除了一个组合框,所有这些工作都很好。如果我在名为“
cmbBodyType”的组合框中输入数据,则无论我输入什么数据,每次都会返回0结果。

到目前为止,我的代码非常冗长,因此我将其全部粘贴在下面,然后粘贴我用于过滤组合框的代码段。

    SELECT [Car Table].Car_VIN, [Car Table].Car_Class, [Car Table].Car_BodyType,
[Car Table].Car_Colour, [Car Table].Car_Make, [Car Table].Car_Model, [Car
Table].Car_EngineType, [Car Table].Car_TransmissionType, [Car
Table].Car_GPSAvailability, [Car Table].Car_BootSpace, [Car
Table].Car_FuelConsumptRate, [Car Table].Car_SeatNumber, [Car
Table].Car_GreenStarRating, [Car Table].Car_ANCAPSafetyRating, [Car
Table].Car_DriveType
FROM [Car Table]

       WHERE ((([Car Table].Car_VIN) Like "*" & [Forms]![Navigation Form]!
    [NavigationSubform].[Form]![txtVIN] & "*") AND (([Car Table].Car_Class) Like "*"
    & [Forms]![Navigation Form]![NavigationSubform].[Form]![cmbClass] & "*") AND
    (([Car Table].Car_DriveType) Like "*" & [Forms]![Navigation Form]!
    [NavigationSubform].[Form]![cmbDriveType] & "*") AND (([Car Table].Car_BodyType)
    Like "*" & [Forms]![Navigation Form]![NavigationSubform].[Form]![cmbBodyType] &
    "*") AND (([Car Table].Car_Colour) Like "*" & [Forms]![Navigation Form]
    ![NavigationSubform].[Form]![cmbBodyType] & "*") AND (([Car Table].Car_Make)
    Like "*" & [Forms]![Navigation Form]![NavigationSubform].[Form]![txtMake] & "*")
    AND (([Car Table].Car_Model) Like "*" & [Forms]![Navigation Form]
    ![NavigationSubform].[Form]![txtModel] & "*") AND (([Car Table].Car_EngineType)
    Like "*" & [Forms]![Navigation Form]![NavigationSubform].[Form]![cmbEngineType]
    & "*") AND (([Car Table].Car_TransmissionType) Like "*" & [Forms]![Navigation
    Form]![NavigationSubform].[Form]![cmbTransmissionType] & "*") AND (([Car
    Table].Car_GPSAvailability) Like "*" & [Forms]![Navigation Form]
    ![NavigationSubform].[Form]![ChGPSAvailability] & "*") AND (([Car
     Table].Car_SeatNumber) Like "*" & [Forms]![Navigation Form]
    ![NavigationSubform].[Form]![txtSeatNumber] & "*") AND (([Car
     Table].Car_GreenStarRating) Like "*" & [Forms]![Navigation Form]
    ![NavigationSubform].[Form]![cmbGreenStarRating] & "*") AND (([Car
     Table].Car_ANCAPSafetyRating) Like "*" & [Forms]![Navigation Form]
    ![NavigationSubform].[Form]![cmbANCAPSafetyRating] & "*") AND
     ((Abs([car_fuelconsumptrate]-Nz([Forms]![Navigation Form]![NavigationSubform]
    .[Form]![txtFuelConsumption],[car_fuelconsumptrate]))<=Nz([2],9999)))
AND ((Abs([car_bootspace]-Nz([Forms]![Navigation Form]![NavigationSubform].[Form]
![txtBootSpace], [car_BootSpace]))<=Nz([100],9999))));

代码段(这将在WHERE中,或者更简单地说,这是在设计视图中显示的critera):

Like "*" & [Forms]![Navigation Form]![NavigationSubform].[Form]![cmbBodyType] & "*"

编辑:看来,无论其价值如何,我的“绿色星级”组合框都不会对搜索产生任何影响。似乎工作和非工作组合框之间的所有内容都是相同的…我注意到的一件事是,两个不工作的组合框都比另一个工作的组合框拥有更多的选择。


阅读 198

收藏
2021-06-07

共1个答案

admin

您可以像这样在WHERE语句中使用一部分

   ...AND [Car Table].Car_BodyType Like 
               IIf(IsNull([Forms]![Navigation Form]![NavigationSubform].[Form]![cmbBodyType]),"*" ,
               [Forms]![Navigation Form]![NavigationSubform].[Form]![cmbBodyType])...

在MS Access搜索表中找到的[需要传递未绑定组合框中的值以进行查询,请阅读该书以获取进一步的说明以及注释。

顺便提一句:

(([Car Table].Car_Colour) Like "*" & [Forms]![Navigation Form]
    ![NavigationSubform].[Form]![cmbBodyType] & "*")

您从cmbBodyType中搜索Car_color

小心这个庞大的SQL

编辑:星级系统:此字段是什么类型?如果它是数字字段,则LIKE不适用。LIKE用于文本afaik。

2021-06-07