以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.com/bbs/index.asp)
--  专家坐堂  (http://foxtable.com/bbs/list.asp?boardid=2)
----  筛选代码能否简化  (http://foxtable.com/bbs/dispbbs.asp?boardid=2&id=186530)

--  作者:HANZHIGANG
--  发布时间:2023/5/9 15:52: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
e.Form.Controls("rdoYifu").Checked = True \' 如果付款状态选择了"已付"
   
If Filter >"" Then
        Filter = Filter &
" And "
   
End If
    Filter = Filter &
"已付款 = True"
End
If
If
e.Form.Controls("rdoWeifu").Checked = True \' 如果付款状态选择了"未付"
    If
Filter >"" Then
       
Filter = Filter & " And "
   
End If
   
Filter = Filter & "已付款 = False Or 已付款 Is Null"
End If
If
Filter > "" Then
    Tables(
"订单").Filter = Filter
End If



帮助文件中窗口筛选的代码,如果把窗口中的控件的名称和表中对应列的名称设置成一样的,能不能把这段代码简化一下。比如用FOR EACH语句实现。最好是能把这段写成一段通用的内部函数就更好了。请老师指导一下



--  作者:有点蓝
--  发布时间:2023/5/9 15:59:00
--  
如果全是字符列,并且都是同一种连接符,比如全是and,或者全部是or。也没有like什么的,就可以for each,否则只能是上面的用法了