根据多个条件将标题插入到 Access 报告中

Posted

技术标签:

【中文标题】根据多个条件将标题插入到 Access 报告中【英文标题】:Inserting title into Access report based on multiple conditions 【发布时间】:2015-05-19 17:44:50 【问题描述】:

我正在编写一份报告,该报告将通过一个表单允许多个过滤器和一个排序。我希望报告的标题能够反映为过滤器和排序所做的选择。

报告可以按 3 个字段过滤:产品类型、收到的可交付成果(是/否)和项目经理。

可以按:合同编号、产品类型、承包商、开始日期、结束日期和收到产品的日期进行排序

我已经让一些元素起作用(那些已选择可交付成果的那些元素已被选中,有无排序),但我遇到了其他问题。

Public Function InsertTitle(strReportName As String) As String
Dim strProduct As String

If strReportName = "rptMasterDeliverables" Then
    If strField = "" Then
        InsertTitle = "All Deliverables Sorted by Project Manager"
    ElseIf Forms!fdlgMasterDeliverables!cboDeliverableReceived Like "No" Then
       InsertTitle = "Outstanding Deliverables Sorted by " & Left(strField, InStr(1, strField, ",") - 1) & ""
    ElseIf Forms!fdlgMasterDeliverables!cboDeliverableReceived Like "Yes" Then     
        InsertTitle = "Received Deliverables Sorted by " & Left(strField, InStr(1, strField, ",") - 1) & ""
    ElseIf InStr(1, strField, ",") = 0 Then                                      
        InsertTitle = "All Deliverables Sorted by " & strField & ""

以上都运行良好,我遇到以下问题

    ElseIf Forms!fdlgMasterDeliverables!cboDeliverableReceived Like "Yes" And Left(strField, 1) = "," Then
        InsertTitle = "Received Deliverables sorted by Project Manager"
    ElseIf Left(strField, 1) = "," And Forms!fdlgMasterDeliverables!cboDeliverableReceived Like "No" Then   'cboReceived
        InsertTitle = "Outstanding Deliverables sorted by Project Manager"
    ElseIf InStr(1, strProduct, ",") = 0 And Forms!fdlgMasterDeliverables!cboDeliverableReceived Like "No" Then
        InsertTitle = "" & Mid([strProduct], 2, Len([strProduct])) & " Deliverables"
    ElseIf Left(strField, 1) = "*" And InStr(1, strField, ",") = 0 Then
        InsertTitle = "All " & Mid([strField], 2, Len([strField])) & " Deliverables sorted by " & strField & ""
    ElseIf Left(strField, 2) = "* " And Forms!fdlgMasterDeliverables!cboDeliverableReceived Like "Yes" Then
        InsertTitle = "Received " & Mid([strField], 3, Len([strField])) & " Deliverables sorted by Project Manager"

打开报表的代码是

Private Sub Report_Open(Cancel As Integer)
Dim strProducts As String
strField = "" 
    If CurrentProject.AllForms("fdlgMasterDeliverables").IsLoaded Then
    If Not IsNull(Forms!fdlgMasterDeliverables!cboSort) Then
        Me.Report.GroupLevel(0).ControlSource = Forms!fdlgMasterDeliverables!cboSort.Column(0)      
        strField = Forms!fdlgMasterDeliverables!cboSort.Column(1)                                 
    If Not IsNull(Forms!fdlgMasterDeliverables!cboDeliverableReceived) Then
        strField = strField & "," & Forms!fdlgMasterDeliverables!cboDeliverableReceived.Column(1)
    End If
    ElseIf Not IsNull(Forms!fdlgMasterDeliverables!cboDeliverableReceived) Then           
        strField = "," & Forms!fdlgMasterDeliverables!cboDeliverableReceived.Column(1)    
    End If
    If Not IsNull(Forms!fdlgMasterDeliverables!cboProductType) Then
        strProduct = Forms!fdlgMasterDeliverables!cboProductType.Column(1)
    End If

目前正常工作的部分返回“按[选定排序]排序的未完成/已收到/所有可交付成果”,如果未选择过滤器或排序,则正确返回“按项目经理排序的所有可交付成果”

我正在尝试使用已识别的过滤器返回标题。例如,如果我选择产品类型 =“文档”和产品经理 =“劳拉”,我希望标题为“劳拉的所有可交付文件”如果我添加收到 =“否”,我希望它阅读“ Laura 出色的文件交付成果”

如果我选择 Received = "Yes" & Product type = "Data" 并选择按合同编号排序,我希望标题为“Received Data Deliverables by Contract Number”

如果没有选择排序顺序,我希望标题默认为“.... 按项目经理排序”

我考虑过使用案例陈述,但不知道从哪里开始。任何对出了什么问题的见解将不胜感激

【问题讨论】:

我应该注意到报告的表现很好,所有过滤器都像排序一样工作......它只是插入了让我发疯的标题 【参考方案1】:

您遇到/看到的错误是什么?看起来您的代码缺少部分,您可以发布所有代码,结果您得到的结果与预期的行为相比吗?

【讨论】:

以上是关于根据多个条件将标题插入到 Access 报告中的主要内容,如果未能解决你的问题,请参考以下文章

报告的多个记录源

使用动态布局在 MS Access 中生成报告

Access 2010:根据特定组合框条件过滤字段中包含多个值的报表

在access vba中输出多个报告会导致错误3014,打开的表太多

创建一个将在表格中显示来自文件名的多个图像的报告

尝试插入 Access 数据库时,条件表达式中的数据类型不匹配错误