以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.net/bbs/index.asp)
--  专家坐堂  (http://foxtable.net/bbs/list.asp?boardid=2)
----  [求助]简化代码  (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=72564)

--  作者:ZDG
--  发布时间:2015/8/1 12:42:00
--  [求助]简化代码

With Tables("台账查询_table1")
    If e.Form.Controls("ComboBox1").text = "" And e.Form.Controls("DateTimePicker1").text ="" And e.Form.Controls("DateTimePicker2").text ="" And e.Form.Controls("DropBox1").text ="" Then
        .filter =""
    Else
        If e.Form.Controls("ComboBox1").text = "" And e.Form.Controls("DateTimePicker1").text ="" And e.Form.Controls("DateTimePicker2").text ="" Then
            .filter = "品号 = \'" & e.Form.Controls("DropBox1").text & "\' "
        Else
            If e.Form.Controls("DateTimePicker1").text ="" And e.Form.Controls("DateTimePicker2").text ="" And e.Form.Controls("DropBox1").text ="" Then
                .filter = "仓库 = \'" & e.Form.Controls("ComboBox1").text & "\' "
            Else
                If  e.Form.Controls("ComboBox1").text = "" And e.Form.Controls("DateTimePicker2").text ="" And e.Form.Controls("DropBox1").text ="" Then
                    .filter ="日期 >= #" &e.Form.Controls("DateTimePicker1").text & "# "
                Else
                    If  e.Form.Controls("ComboBox1").text = "" And e.Form.Controls("DateTimePicker1").text ="" And e.Form.Controls("DropBox1").text ="" Then
                        .filter = "日期 <= #" & e.Form.Controls("DateTimePicker2").text & "# "
                    Else
                        If e.Form.Controls("DateTimePicker1").text ="" And e.Form.Controls("DateTimePicker2").text ="" Then
                           
                            .filter = "仓库 = \'" & e.Form.Controls("ComboBox1").text & "\'and 品号 =\'" & e.Form.Controls("DropBox1").text & "\' "
                        Else
                            If e.Form.Controls("DropBox1").text="" And e.Form.Controls("DateTimePicker1").text="" Then
                                .filter = "仓库 = \'" & e.Form.Controls("ComboBox1").text & "\' and 日期 <= #" & e.Form.Controls("DateTimePicker2").text & "# "
                            Else
                                If e.Form.Controls("DateTimePicker2").text="" And e.Form.Controls("DropBox1").text="" Then
                                    .filter = "仓库 = \'" & e.Form.Controls("ComboBox1").text & "\' and 日期 >= #" & e.Form.Controls("DateTimePicker1").text & "# "
                                Else
                                    If e.Form.Controls("DropBox1").text ="" And e.Form.Controls("ComboBox1").text="" Then
                                        .filter ="日期 >= #" &e.Form.Controls("DateTimePicker1").text & "# and 日期 <= #" & e.Form.Controls("DateTimePicker2").text & "# "
                                    Else
                                        If e.Form.Controls("DateTimePicker1").text="" And e.Form.Controls("ComboBox1").text="" Then
                                            .filter = "日期 <= #" & e.Form.Controls("DateTimePicker2").text & "# and 品号 =\'" & e.Form.Controls("DropBox1").text & "\' "
                                        Else
                                            If e.Form.Controls("DateTimePicker1").text="" And e.Form.Controls("ComboBox1").text="" Then
                                                .filter = "日期 >= #" & e.Form.Controls("DateTimePicker1").text & "# and 品号 =\'" & e.Form.Controls("DropBox1").text & "\' "
                                            Else
                                               
                                               
                                                If e.Form.Controls("DropBox1").text ="" Then
                                                    .filter = "仓库 = \'" & e.Form.Controls("ComboBox1").text & "\' and 日期 >= #" &e.Form.Controls("DateTimePicker1").text & "# and 日期<= #" & e.Form.Controls("DateTimePicker2").text & "# "
                                                Else
                                                    If e.Form.Controls("ComboBox1").text = "" Then
                                                        .filter = "日期 >= #" &e.Form.Controls("DateTimePicker1").text & "# and 日期<= #" & e.Form.Controls("DateTimePicker2").text & "# and 品号 = \'" & e.Form.Controls("DropBox1").text & "\' "
                                                    Else
                                                        If e.Form.Controls("DateTimePicker1").text="" Then
                                                            .filter = "仓库 = \'" & e.Form.Controls("ComboBox1").text & "\' and 日期<= #" & e.Form.Controls("DateTimePicker2").text & "# and 品号 = \'" & e.Form.Controls("DropBox1").text & "\' "
                                                           
                                                        Else
                                                            If e.Form.Controls("DateTimePicker2").text="" Then
                                                                .filter = "仓库 = \'" & e.Form.Controls("ComboBox1").text & "\' and 日期 >= #" &e.Form.Controls("DateTimePicker1").text & "# and 品号 = \'" & e.Form.Controls("DropBox1").text & "\' "
                                                            Else
                                                                .filter = "仓库 = \'" & e.Form.Controls("ComboBox1").text & "\' and 日期 >= #" &e.Form.Controls("DateTimePicker1").text & "# and 日期<= #" & e.Form.Controls("DateTimePicker2").text & "# and 品号 = \'" & e.Form.Controls("DropBox1").text & "\' "
                                                            End If
                                                        End If
                                                    End If
                                                End If
                                            End If
                                        End If
                                    End If
                                End If
                            End If
                        End If
                    End If
                End If
            End If
        End If
    End If

End With

 

那位大神能帮忙简化下代码


--  作者:y2287958
--  发布时间:2015/8/1 13:09:00
--  
http://www.foxtable.com/help/topics/1058.htm
--  作者:逛逛
--  发布时间:2015/8/1 13:45:00
--  
Dim filter As String  = ""
If e.Form.Controls("ComboBox1").text > ""  Then  filter = "仓库 = \'" & e.Form.Controls("ComboBox1").text & "\' "
If e.Form.Controls("DateTimePicker1").text >"" Then  filter =filter & " And 日期 >= #" & e.Form.Controls("DateTimePicker1").text & "# "
If e.Form.Controls("DateTimePicker2").text >""  Then  filter =filter & " And 日期 <= #" & e.Form.Controls("DateTimePicker2").text & "#  "
If e.Form.Controls("DropBox1").text >""  Then  filter =filter & " And 品号 =\'" & e.Form.Controls("DropBox1").text & "\' "
filter = filter.Trim
If filter.StartsWith("And") Then  filter = filter.SubString(3)
Tables("台账查询_table1").Filter = filter
--  作者:ZDG
--  发布时间:2015/8/1 13:51:00
--  
谢谢,这个好,我都写死了
--  作者:xuebai
--  发布时间:2015/10/16 13:44:00
--  
学习了,另外弱弱的问一下,If filter.StartsWith("And") Then  filter = filter.SubString(3) 中,参数3是什么含义?
--  作者:大红袍
--  发布时间:2015/10/16 14:01:00
--  
以下是引用xuebai在2015/10/16 13:44:00的发言:
学习了,另外弱弱的问一下,If filter.StartsWith("And") Then  filter = filter.SubString(3) 中,参数3是什么含义?

 

参考,从第三位开始截取所有字符

 

http://www.foxtable.com/help/topics/0240.htm