以下代码是范例『窗口筛选』的代码,虽然简单明白,不过有点啰嗦。
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
[此贴子已经被作者于2008-10-25 13:23:07编辑过]