以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.net/bbs/index.asp)
--  专家坐堂  (http://foxtable.net/bbs/list.asp?boardid=2)
----  [原创]将简洁进行到底:简化范例『窗口筛选』的代码  (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=923)

--  作者:don
--  发布时间:2008/10/20 12:53:00
--  [原创]将简洁进行到底:简化范例『窗口筛选』的代码
      以下代码是范例『窗口筛选』的代码,虽然简单明白,不过有点啰嗦

Dim Filter As String
With e.Form.Controls("cmbProduct")
    If .Value IsNot Nothing Then
        Filter = "产品 = \'" & .Value & "\'"
    End If
End With
With e.Form.Controls("cmbCustomer")
    If .Value IsNot Nothing Then
        If Filter > "" Then
            Filter = Filter & " And "
        End If
        Filter = Filter & "客户 = \'" & .Value & "\'"
    End If
End With
With e.Form.Controls("cmbEmployee")
    If .Value IsNot Nothing Then
        If Filter >"" Then
            Filter = Filter & " And "
        End If
        Filter = Filter & "雇员 = \'" & .Value & "\'"
    End If
End With
With e.Form.Controls("StartDate")
    If .Value IsNot Nothing Then
        If Filter >"" Then
            Filter = Filter & " And "
        End If
        Filter = Filter & "日期 >= #" & .Value & "#"
    End If
End With
With e.Form.Controls("EndDate")
    If .Value IsNot Nothing Then
        If Filter >"" Then
            Filter = Filter & " And "
        End If
        Filter = Filter & "日期 <= #" & .Value & "#"
    End If
End With
If Filter > "" Then
    Tables("订单").Filter = Filter
End If

本人较喜欢简洁点的公式,于是对以上代码进行简化:

筛选代码:
Dim T As String() = {"CP","KH","GY","SD","ED","产品","客户","雇员","起始日期","结束日期"}
Dim Ft As String
    For n As Integer = 0 to 4
        dim Tc As String = e.form.Controls(T(n)).Value
        if Tc > "" then
           if n = 3 Then       
               Ft = Ft & " And " & T(n+5) & " >= #" & Tc & "#"
           ElseIF n = 4 Then 
               Ft = Ft & " And " & T(n+5) & " <= #" & Tc & "#"
           Else
               Ft = Ft & " And " & T(n+5) & " = \'" & Tc & "\'"
           End If
       End If
Next
If Ft > "" Then
      Tables("订单").Filter = Ft.SubString(5)
End If


清除条件代码:
Dim T As String() = {"CP","KH","GY","SD","ED"}
For n As Integer = 0 to 4
     e.Form.Controls(T(n)).Value = Nothing
Next



 下载信息  [文件大小:   下载次数: ]
图片点击可在新窗口打开查看点击浏览该文件:窗口筛选.table

[此贴子已经被作者于2008-10-25 13:23:07编辑过]

--  作者:狐狸爸爸
--  发布时间:2008/10/20 12:57:00
--  

呵呵,我也学一下


--  作者:czy
--  发布时间:2008/10/20 13:03:00
--  
大师风采依旧,学习。
--  作者:gdlgh
--  发布时间:2008/10/20 13:18:00
--  
顶一个!
--  作者:smileboy
--  发布时间:2008/10/20 13:21:00
--  
大师级图片点击可在新窗口打开查看
--  作者:ylm
--  发布时间:2008/10/20 21:29:00
--  
开眼界了,谢谢大师!
--  作者:zhuxinhui
--  发布时间:2018/11/29 10:51:00
--  
好东西一定要好好学习