注册 登录
Office中国论坛/Access中国论坛 返回首页

的个人空间 http://www.office-cn.net/?0 [收藏] [复制] [分享] [RSS]

日志

窗体多条件筛选函数

已有 355 次阅读2008-1-25 12:39 |个人分类:VBA

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

评论 (0 个评论)

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 注册

QQ|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )  

GMT+8, 2024-5-30 04:56 , Processed in 0.048708 second(s), 14 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

返回顶部