如何在报表过滤器上组合两个字符串?

时间:2016-03-05 16:32:40

标签: ms-access vba

我是第一次在Access中为应用程序编写VBA代码,并创建了两个单独的字符串来过滤报表。这些strFilter中的第一个根据列表框中的条件过滤报告。第二个问题已设置为根据输入到一对文本框中的日期过滤报告。当单独使用时,这两个字符串过滤器都可以正常工作。

我想知道的是,是否有办法轻松组合这两个字符串,以便用户根据列表框中的条件和它们在文本框中输入的日期过滤报告。

将列表框过滤器添加到报表过滤器时,我的代码如下所示:

With Reports![rptFaultRecords]
    .Filter = strFilter
    .FilterOn = True

我想添加字符串以在strFilter旁边的日期strWhere过滤,以便可以按日期和列表条件过滤报告。我尝试执行此操作时输入的所有代码都给出了运行时错误3075.这两个字符串是否可以轻松组合,如果可以,我该怎么办?

如果您需要查看,我写的其余代码如下:

Private Sub btnAllFaultsFilter_Click()
Dim varItem As Variant
Dim strRoom As String   
Dim strFilter As String
Dim strDevice As String
Dim strCat As String
Dim strStatus As String
Dim strDateField As String
Dim strWhere As String
Const strcJetDate = "\#mm\/dd\/yyyy\#"

strDateField = "[f_datereported]"

    If IsDate(Me.txtStartDate) Then
    strWhere = "(" & strDateField & " >= " & Format(Me.txtStartDate, strcJetDate) & ")"
End If
If IsDate(Me.txtEndDate) Then
    If strWhere <> vbNullString Then
        strWhere = strWhere & " AND "
    End If
    strWhere = strWhere & "(" & strDateField & " < " & Format(Me.txtEndDate + 1, strcJetDate) & ")"
End If   


For Each varItem In Me.lstRoom.ItemsSelected
    strRoom = strRoom & ",'" & Me.lstRoom.ItemData(varItem) & "'"
    Next varItem
    If Len(strRoom) = 0 Then
        strRoom = "Like '*'"
    Else
        strRoom = Right(strRoom, Len(strRoom) - 1)
        strRoom = "IN(" & strRoom & ")"
    End If
For Each varItem In Me.lstDevice.ItemsSelected
    strDevice = strDevice & ",'" & Me.lstDevice.ItemData(varItem) & "'"
    Next varItem
    If Len(strDevice) = 0 Then
        strDevice = "Like '*'"
    Else
        strDevice = Right(strDevice, Len(strDevice) - 1)
        strDevice = "IN(" & strDevice & ")"
    End If
For Each varItem In Me.lstCategory.ItemsSelected
    strCat = strCat & ",'" & Me.lstCategory.ItemData(varItem) & "'"
    Next varItem
    If Len(strCat) = 0 Then
        strCat = "Like '*'"
    Else
        strCat = Right(strCat, Len(strCat) - 1)
        strCat = "IN(" & strCat & ")"
    End If
For Each varItem In Me.lstStatus.ItemsSelected
    strStatus = strStatus & ",'" & Me.lstStatus.ItemData(varItem) & "'"
    Next varItem
    If Len(strStatus) = 0 Then
        strStatus = "Like '*'"
    Else
        strStatus = Right(strStatus, Len(strStatus) - 1)
        strStatus = "IN(" & strStatus & ")"
    End If
strFilter = "[c_roomid] " & strRoom & " AND [f_computername] " & strDevice & " AND [f_faultcategory] " & strCat & " AND [f_faultstatus] " & strStatus

With Reports![rptFaultRecords]
    '.Filter = strFilter
    .Filter = strFilter
    .FilterOn = True

End With
End Sub

0 个答案:

没有答案
相关问题