Foxtable(狐表)用户栏目专家坐堂 → [原创]将简洁进行到底:简化范例『窗口筛选』的代码


  共有18501人关注过本帖树形打印复制链接

主题:[原创]将简洁进行到底:简化范例『窗口筛选』的代码

帅哥哟,离线,有人找我吗?
don
  1楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:八尾狐 帖子:1812 积分:12999 威望:0 精华:14 注册:2008/10/11 18:07:00
[原创]将简洁进行到底:简化范例『窗口筛选』的代码  发帖心情 Post By: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编辑过]

[本帖被加为精华]
 回到顶部