在访问Vba。筛选查询

时间:2018-08-21 18:54:19

标签: vba ms-access

我在访问vba时遇到问题。我编写了代码来过滤表单并通过按钮生成报告。现在,当我运行查询时,如果将报表与表单放在同一页上,它将仅过滤并生成报表。有没有一种方法可以分开创建我的报告?

Private Sub Search_Click()
    Dim strWhere As String
    Dim i As Variant
    Dim varItem As Variant
    Dim strDelim As String

    If Nz(Me.txtFullName, "") <> "" Then
        strWhere = strWhere & "FullName Like '*" & Replace(Me.txtFullName, "'", "''") & "*' AND "
    End If

    If Nz(Me.txtCivil, "") <> "" Then
        strWhere = strWhere & "CivilService_Status Like '*" & Replace(Me.txtCivil, "'", "''") & "*' AND "
    End If

    If Nz(Me.txtOffice, "") <> "" Then
        strWhere = strWhere & "Office_Title Like '*" & Replace(Me.txtOffice, "'", "''") & "*' AND "
    End If

    If Nz(Me.cboPosition, "") <> "" Then
        strWhere = strWhere & "[Full Time],[Part Time] = '" & Me.cboPosition.Value & "' AND "
    End If

    If Nz(Me.txtStartdate, "") <> "" Then
        strWhere = strWhere & "AgencyStart_Date Like '*" & Replace(Me.txtStartdate, "'", "''") & "*' AND "
    End If

    For Each i In Me.lstBureau.ItemsSelected 'listbox
        If Nz(Me.txtdivision, "") <> "" Then
            strWhere = strWhere & "Division Like '*" & Replace(Me.txtdivision, "'", "''") & "*' AND "
        End If
    Next i

    If strWhere <> "" Then
        strWhere = Left(strWhere, Len(strWhere) - 5)
        Report_Onboard_summary.Filter = strWhere
        Report_Onboard_summary.FilterOn = True
        'DoCmd.OpenReport "Onboard_summary", acViewReport, strWhere
    Else
        Report_Onboard_summary.Filter = ""
        Report_Onboard_summary.FilterOn = False
    End If
End Sub

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:0)

我通常使用OpenReport命令打开报告。

DoCmd.OpenReport "Report_Onboard_summary", acViewPreview, WhereCondition:=strWhere 

预览,或

DoCmd.OpenReport "Report_Onboard_summary", acViewNormal, WhereCondition:=strWhere 

立即打印。

这假设报表是作为单独存储在数据库中的Report对象而不是Form对象创建的。您说您将其存储在同一页面上,这使我相信您拥有一个子表单,而不是实际的Report