当搜索字段为空时,强制搜索表单中的空查询

时间:2017-02-02 18:28:22

标签: sql database ms-access filter wildcard

我创建了一个搜索表单,该表单使用其中的字段来过滤包含的子表单。这样可以正常工作,除非搜索字段为空白,查询中的所有记录都会显示。

我希望用户在开始填写搜索条件之前看不到任何记录(仅当搜索字段为空时)。

注意:并非所有搜索条件都是必需的,因此在使用表单时某些字段可能为空。

另一个注意事项:我不是要过滤掉查询中的空白字段。

*** 编辑: 我有一张表格,让我们称之为frmA。在这种形式中有一个子表单,我们称之为sfmB。 sfmB的记录源是我们称之为qryB的查询。

示例查询(再次按原样运行):

SELECT FieldA, FieldB, FieldC, FieldD, FieldE
FROM tblA
WHERE (FieldA Like "*" & [Forms]![frmA]![FieldA] & "*") 
  AND (FieldB Like "*" & [Forms]![frmA]![FieldB] & "*") AND ...;

尝试过:

WHERE (IIf(IsNull([Forms]![frmA]![FieldA])=True, "", FieldA 
                  Like "*" & [Forms]![frmA]![FieldA] & "*"))...

也尝试过:

WHERE (IIf([Forms]![frmA]![FieldA])="", "", FieldA 
           Like "*" & [Forms]![frmA]![FieldA] & "*")...

也尝试过:

WHERE (IIf([Forms]![frmA]![FieldA])=Null, "", FieldA 
           Like "*" & [Forms]![frmA]![FieldA] & "*")...

3 个答案:

答案 0 :(得分:0)

试试这个:

reactjs

答案 1 :(得分:0)

原因所有记录返回的是空表单控件返回任何值,因为您没有与通配符串联,因此在**评估中返回此搜索模式LIKE。如下所示,空字符串或空值,此搜索模式返回任何非空字符的所有记录。

SELECT FieldA, FieldB, FieldC, FieldD, FieldE
FROM tblA
WHERE (FieldA Like "*" & '' & "*") 
  AND (FieldB Like "*" & '' & "*") AND ...;    

SELECT FieldA, FieldB, FieldC, FieldD, FieldE
FROM tblA
WHERE (FieldA Like "*" & NULL & "*") 
  AND (FieldB Like "*" & NULL & "*") AND ...;

要解决此问题,请考虑使用NZ()替换来连接不会显示在您的列中的字符。现在,下面的逻辑将按原样按表单搜索值返回所有记录。但是,如果表单值为空,NZ()将替换为~,并且列中包含此字符的所有记录都将返回。假设该列中不存在tilda,则不输出任何记录。

SELECT FieldA, FieldB, FieldC, FieldD, FieldE
FROM tblA
WHERE (FieldA Like "*" & NZ([Forms]![frmA]![FieldA], '~') & "*") 
  AND (FieldB Like "*" & NZ([Forms]![frmA]![FieldB], '~') AND ...;

要完全实现,必须动态替换NZ()替换值。因此,请考虑按互斥条件纳入VBA:

  1. 当所有字段都为空时,NZ()应使用tilda(~)。

  2. 当至少一个字段非空时,所有剩余的空字段“NZ()应使用通配符运算符星号(*)。

  3. Dim i As Integer, num_search_fields As Integer
    Dim strSQL As String
    Dim var As Variant
    
    i = 0
    num_search_fields = 5
    ' LOOP THROUGH ALL SEARCH FIELDS
    For Each var in Array("FieldA", "FieldB", "FieldC", "FieldD", "FieldE")
        If IsNull(Forms("frmA").Controls(var).Value) Then
             i = i + 1
        End If
    Next var
    
    ' CONDITIONALLY BUILD SQL
    If i = num_search_fields Then
          strSQL = "SELECT FieldA, FieldB, FieldC, FieldD, FieldE" _
                     & " FROM tblA" _
                     & " WHERE (FieldA Like '*' & NZ([Forms]![frmA]![FieldA], '~') & '*')" _
                     & " AND (FieldB Like '*' & NZ([Forms]![frmA]![FieldB], '~') & '*')" _
                     & " AND (FieldC Like '*' & NZ([Forms]![frmA]![FieldC], '~') & '*')" _
                     & " AND (FieldD Like '*' & NZ([Forms]![frmA]![FieldD], '~') & '*')" _
                     & " AND (FieldE Like '*' & NZ([Forms]![frmA]![FieldE], '~') & '*')" 
    Else
          strSQL = "SELECT FieldA, FieldB, FieldC, FieldD, FieldE" _
                     & " FROM tblA" _
                     & " WHERE (FieldA Like '*' & NZ([Forms]![frmA]![FieldA], '*') & '*')" _
                     & " AND (FieldB Like '*' & NZ([Forms]![frmA]![FieldB], '*') & '*')" _
                     & " AND (FieldC Like '*' & NZ([Forms]![frmA]![FieldC], '*') & '*')" _
                     & " AND (FieldD Like '*' & NZ([Forms]![frmA]![FieldD], '*') & '*')" _
                     & " AND (FieldE Like '*' & NZ([Forms]![frmA]![FieldE], '*') & '*')" 
    End If
    
    ' ASSIGN SQL STRING TO SUBFORM RECORDSOURCE
    Forms!frmA!subform.Form.RecordSource = strSQL
    Forms!frmA!subform.Form.Requery
    

答案 2 :(得分:0)

如果您希望在所有搜索条件为空时没有返回任何记录,则必须在WHERE子句中明确地单独测试此条件。类似的东西:

SELECT FieldA, FieldB, FieldC, FieldD, FieldE
FROM tblA
WHERE 
  NOT ( IsNull([Forms]![frmA]![FieldA]) 
        AND IsNull([Forms]![frmA]![FieldB]) 
        AND IsNull([Forms]![frmA]![FieldC]) 
        AND IsNull([Forms]![frmA]![FieldD]) 
        AND IsNull([Forms]![frmA]![FieldE]) )
  AND (FieldA Like "*" & [Forms]![frmA]![FieldA] & "*") 
  AND (FieldB Like "*" & [Forms]![frmA]![FieldB] & "*") AND ...;

因此,如果frmA上的所有搜索字段都为空,那么NOT (...)括号内的条件将为TRUENOT会将其转换为FALSE,因此您的整个WHERE子句将为FALSE,并且不会返回任何行。如果搜索字段中有值,那么NOT (...)内的FALSE将为NOTTRUE会将此WHERE转为def get_package_info(): info_dict = {} with open(os.path.join(glob.glob("./*.egg-info")[0], "PKG-INFO"), "r") as info: for i in info: i = i.split(":") info_dict[i[0].strip()] = i[1].strip() return info_dict ,然后其他条件会应用[2017-02-03T03:05:35,049][INFO ][logstash.pipeline ] Pipeline main started [2017-02-03T03:05:35,064][DEBUG][logstash.agent ] Starting puma [2017-02-03T03:05:35,065][DEBUG][logstash.agent ] Trying to start WebServer {:port=>9600} [2017-02-03T03:05:35,068][DEBUG][logstash.api.service ] [api-service] start [2017-02-03T03:05:35,090][INFO ][logstash.agent ] Successfully started Logstash API endpoint {:port=>9600} [2017-02-03T03:05:35,303][DEBUG][logstash.inputs.kafka ] closing {:plugin=>"LogStash::Inputs::Kafka"} [2017-02-03T03:05:35,304][DEBUG][logstash.pipeline ] Input plugins stopped! Will shutdown filter/output workers. [2017-02-03T03:05:35,338][DEBUG][logstash.pipeline ] Pushing flush onto pipeline [2017-02-03T03:05:35,339][DEBUG][logstash.pipeline ] Pushing shutdown {:thread=>"#<Thread:0x6e057136 sleep>"} [2017-02-03T03:05:35,340][DEBUG][logstash.pipeline ] Pushing shutdown {:thread=>"#<Thread:0xcb2b987 sleep>"} [2017-02-03T03:05:35,340][DEBUG][logstash.pipeline ] Pushing shutdown {:thread=>"#<Thread:0x6aa67ce5 sleep>"} [2017-02-03T03:05:35,340][DEBUG][logstash.pipeline ] Pushing shutdown {:thread=>"#<Thread:0x2f544881 run>"} [2017-02-03T03:05:35,340][DEBUG][logstash.pipeline ] Pushing shutdown {:thread=>"#<Thread:0x20d253d0 sleep>"} [2017-02-03T03:05:35,341][DEBUG][logstash.pipeline ] Pushing shutdown {:thread=>"#<Thread:0x65d168b sleep>"} [2017-02-03T03:05:35,341][DEBUG][logstash.pipeline ] Shutdown waiting for worker thread #<Thread:0x6e057136> [2017-02-03T03:05:35,439][FATAL][logstash.runner ] An unexpected error occurred! {:error=>#<ArgumentError: negative length -2600952 given>, :backtrace=>["org/jruby/ext/stringio/StringIO.java:829:in `read'", "/apps/logstash-5.2.0/vendor/bundle/jruby/1.9/gems/avro-1.8.1/lib/avro/io.rb:106:in `read'", "/apps/logstash-5.2.0/vendor/bundle/jruby/1.9/gems/avro-1.8.1/lib/avro/io.rb:93:in `read_bytes'", "/apps/logstash-5.2.0/vendor/bundle/jruby/1.9/gems/avro-1.8.1/lib/avro/io.rb:304:in `read_data'", "/apps/logstash-5.2.0/vendor/bundle/jruby/1.9/gems/avro-1.8.1/lib/avro/io.rb:290:in `read_data'", "/apps/logstash-5.2.0/vendor/bundle/jruby/1.9/gems/avro-1.8.1/lib/avro/io.rb:376:in `read_union'", "/apps/logstash-5.2.0/vendor/bundle/jruby/1.9/gems/avro-1.8.1/lib/avro/io.rb:309:in `read_data'", "/apps/logstash-5.2.0/vendor/bundle/jruby/1.9/gems/avro-1.8.1/lib/avro/io.rb:384:in `read_record'", "org/jruby/RubyArray.java:1613:in `each'", "/apps/logstash-5.2.0/vendor/bundle/jruby/1.9/gems/avro-1.8.1/lib/avro/io.rb:382:in `read_record'", "/apps/logstash-5.2.0/vendor/bundle/jruby/1.9/gems/avro-1.8.1/lib/avro/io.rb:310:in `read_data'", "/apps/logstash-5.2.0/vendor/bundle/jruby/1.9/gems/avro-1.8.1/lib/avro/io.rb:275:in `read'", "/apps/logstash-5.2.0/vendor/bundle/jruby/1.9/gems/logstash-codec-avro-3.0.0-java/lib/logstash/codecs/avro.rb:73:in `decode'", "/apps/logstash-5.2.0/vendor/bundle/jruby/1.9/gems/logstash-input-kafka-4.1.1/lib/logstash/inputs/kafka.rb:163:in `thread_runner'", "file:/apps/logstash-5.2.0/vendor/jruby/lib/jruby.jar!/jruby/java/java_ext/java.lang.rb:12:in `each'", "/apps/logstash-5.2.0/vendor/bundle/jruby/1.9/gems/logstash-input-kafka-4.1.1/lib/logstash/inputs/kafka.rb:162:in `thread_runner'"]} [2017-02-03T03:05:35,444][DEBUG][logstash.agent ] Error in reactor loop escaped: Bad file descriptor - Bad file descriptor (Errno::EBADF) [2017-02-03T03:05:35,445][DEBUG][logstash.agent ] ["org/jruby/RubyIO.java:3705:in `select'", "/apps/logstash-5.2.0/vendor/bundle/jruby/1.9/gems/puma-2.16.0-java/lib/puma/reactor.rb:29:in `run_internal'", "/apps/logstash-5.2.0/vendor/bundle/jruby/1.9/gems/puma-2.16.0-java/lib/puma/reactor.rb:138:in `run_in_thread'"] [2017-02-03T03:05:35,445][DEBUG][logstash.agent ] 2017-02-03 03:05:35 -0500: Listen loop error: #<Errno::EBADF: Bad file descriptor - Bad file descriptor> [2017-02-03T03:05:35,446][DEBUG][logstash.agent ] org/jruby/RubyIO.java:3705:in `select' /apps/logstash-5.2.0/vendor/bundle/jruby/1.9/gems/puma-2.16.0-java/lib/puma/server.rb:322:in `handle_servers' /apps/logstash-5.2.0/vendor/bundle/jruby/1.9/gems/puma-2.16.0-java/lib/puma/server.rb:296:in `run' [2017-02-03T03:05:35,446][DEBUG][logstash.agent ] Error in reactor loop escaped: Bad file descriptor - Bad file descriptor (Errno::EBADF) 条款将会发挥作用。

编辑 - 我刚刚重新阅读了Parfait的答案,实际上我认为这是一个更简洁的解决方案!