|
Function GetFilterStr(strFilter As String, strField As String, intType As Integer, varArg As Variant) As String
'调用的时候需要传递窗体的原筛选条件, 新筛选条件的字段, 字段的类型(数字, 字符串, 日期, 新筛选条件的参数)
Dim strNewFilter As String
Dim aryFilter As Variant
Dim strFilterSplitted As String
Dim intCount As Integer
Dim intUbound As Integer
intUbound = -1
aryFilter = Split(strFilter, "AND") '以AND为间隔分离原窗体的筛选条件
intUbound = UBound(aryFilter)
For intCount = 0 To intUbound
strFilterSplitted = Trim(aryFilter(intCount))
If Left(strFilterSplitted, 1) = "#" Then
aryFilter(intCount) = Trim(aryFilter(intCount - 1)) & " AND " & strFilterSplitted
aryFilter(intCount - 1) = ""
strFilterSplitted = Trim(aryFilter(intCount))
End If
'对于日期型条件由于有BETWEEN...AND..., 所以将AND后面的日期参数和前面的条件再重新合并.
If InStr(strFilterSplitted, strField) > 0 Then
aryFilter(intCount) = ""
If InStr(strFilterSplitted, "#") > 0 Then
aryFilter(intCount + 1) = ""
End If
End If
'判断原筛选条件是否有包含新的筛选字段, 如果有, 则删除原条件中相关的字段参数, 如果是日期型的还要删除下一条(AND后面的日期参数)
Next
Select Case intType '数字,字符,日期
Case 1 '数字型
If varArg > 0 Then
strNewFilter = strField & "=" & varArg
Else
strNewFilter = ""
End If
Case 2 '字符串型
If varArg = "所有" Then
strNewFilter = ""
Else
strNewFilter = strField & "='" & varArg & "'"
End If
Case 3 '日期类型1
If varArg = "所有" Then
strNewFilter = ""
Else
strNewFilter = strField & " between " & varArg
End If
End Select
'根据不同的类型生成新的筛选条件. 对于数字型参数0表示所有, 对于字符串型和日期型传递"所有"表示显示所有,
If Len(strNewFilter) > 0 Then
GetFilterStr = strNewFilter
End If
For intCount = 0 To intUbound
strFilterSplitted = Trim(aryFilter(intCount))
If Len(strFilterSplitted) > 0 Then
GetFilterStr = strFilterSplitted & " AND " & GetFilterStr
End If
Next
GetFilterStr = Trim(GetFilterStr)
If Right(Trim(GetFilterStr), 3) = "AND" Then
GetFilterStr = Trim(Left(GetFilterStr, Len(GetFilterStr) - 3))
End If
'合并原筛选条件, 生成新的筛选条件
End Function |
评分
-
查看全部评分
|