如何在访问报告中生成列表?

Posted

技术标签:

【中文标题】如何在访问报告中生成列表?【英文标题】:How to generate a list in an Access Report? 【发布时间】:2021-06-25 03:00:49 【问题描述】:

我正在尝试在 Microsoft Access 中生成一份报告,该报告应根据名为 Checkup 的表中名为 Exams 的字段中存储的内容创建一个列表。

到目前为止,我有一个带有计算字段的报告和一个带有记录源考试的文本框。计算字段使用 Switch 函数生成列表;这是代码:

=Switch([Examenes]="BHC","BHC" & Chr(13) & Chr(10) & "Hto:" & Chr(13) & Chr(10) & "Gb:" & Chr(13) & Chr(10) & "E:" & Chr(13) & Chr(10) & "S:" & Chr(13) & Chr(10) & "L:" & Chr(13) & Chr(10) & "M:" & Chr(13) & Chr(10) & "St:" & Chr(13) & Chr(10) & "B:",[Examenes]="BHC, VDRL","BHC" & Chr(13) & Chr(10) & "Hto:" & Chr(13) & Chr(10) & "Gb:" & Chr(13) & Chr(10) & "E:" & Chr(13) & Chr(10) & "S:" & Chr(13) & Chr(10) & "L:" & Chr(13) & Chr(10) & "M:" & Chr(13) & Chr(10) & "St:" & Chr(13) & Chr(10) & "B:" & Chr(13) & Chr(10) & "VDRL:",[Examenes]="BHC, EGO, VDRL","BHC" & Chr(13) & Chr(10) & "Hto:" & Chr(13) & Chr(10) & "GB:" & Chr(13) & Chr(10) & "E:" & Chr(13) & Chr(10) & "S:" & Chr(13) & Chr(10) & "L:" & Chr(13) & Chr(10) & "M:" & Chr(13) & Chr(10) & "St:" & Chr(13) & Chr(10) & "B:" & Chr(13) & Chr(10) & "VDRL:" & Chr(13) & Chr(10) & "EGO" & Chr(13) & Chr(10) & "Color:" & Chr(13) & Chr(10) & "Densidad:" & Chr(13) & Chr(10) & "Ph:" & Chr(13) & Chr(10) & "SO:" & Chr(13) & Chr(10) & "Proteinas:" & Chr(13) & Chr(10) & "CE:" & Chr(13) & Chr(10) & "LL:" & Chr(13) & Chr(10) & "FM:" & Chr(13) & Chr(10) & "Nitrito:" & Chr(13) & Chr(10) & "Cristales:",[Examenes]="BHC, EGH, EGO, VDRL","BHC" & Chr(13) & Chr(10) & "Hto:" & Chr(13) & Chr(10) & "Gb:" & Chr(13) & Chr(10) & "E:" & Chr(13) & Chr(10) & "S:" & Chr(13) & Chr(10) & "L:" & Chr(13) & Chr(10) & "M:" & Chr(13) & Chr(10) & "St:" & Chr(13) & Chr(10) & "B:" & Chr(13) & Chr(10) & "VDRL:" & Chr(13) & Chr(10) & "EGO" & Chr(13) & Chr(10) & "Color:" & Chr(13) & Chr(10) & "Densidad:" & Chr(13) & Chr(10) & "Ph:" & Chr(13) & Chr(10) & "SO:" & Chr(13) & Chr(10) & "Proteinas:" & Chr(13) & Chr(10) & "CE:" & Chr(13) & Chr(10) & "LL:" & Chr(13) & Chr(10) & "FM:" & Chr(13) & Chr(10) & "Nitrito:" & Chr(13) & Chr(10) & "Cristales:" & Chr(13) & Chr(10) & "EGH" & Chr(13) & Chr(10) & "Protozoarios:" & Chr(13) & Chr(10) & "Metazoarios:")

代码运行良好,但是,我需要向 Switch 函数添加更多值,但我收到错误消息“文本太长,无法编辑”。由于这个字段是计算出来的,我不能只是去表格添加更多字符或将其变成备忘录类型字段(或者我至少不知道如何)。

这个问题有解决方法吗?我还尝试将代码放在报告中的 onLoad(或 OnOpen)事件中,但没有让它工作。访问给我编译错误:预期:列表分隔符或)

代码如下:

Private Sub Report_Open(Cancel As Integer)
Switch([Examenes]="BHC","BHC" & Chr(13) & Chr(10) & "Hto:" & Chr(13) & Chr(10) & "Gb:" & Chr(13) & Chr(10) & "E:" & Chr(13) & Chr(10) & "S:" & Chr(13) & Chr(10) & "L:" & Chr(13) & Chr(10) & "M:" & Chr(13) & Chr(10) & "St:" & Chr(13) & Chr(10) & "B:",[Examenes]="BHC, VDRL","BHC" & Chr(13) & Chr(10) & "Hto:" & Chr(13) & Chr(10) & "Gb:" & Chr(13) & Chr(10) & "E:" & Chr(13) & Chr(10) & "S:" & Chr(13) & Chr(10) & "L:" & Chr(13) & Chr(10) & "M:" & Chr(13) & Chr(10) & "St:" & Chr(13) & Chr(10) & "B:" & Chr(13) & Chr(10) & "VDRL:",[Examenes]="BHC, EGO, VDRL","BHC" & Chr(13) & Chr(10) & "Hto:" & Chr(13) & Chr(10) & "GB:" & Chr(13) & Chr(10) & "E:" & Chr(13) & Chr(10) & "S:" & Chr(13) & Chr(10) & "L:" & Chr(13) & Chr(10) & "M:" & Chr(13) & Chr(10) & "St:" & Chr(13) & Chr(10) & "B:" & Chr(13) & Chr(10) & "VDRL:" & Chr(13) & Chr(10) & "EGO" & Chr(13) & Chr(10) & "Color:" & Chr(13) & Chr(10) & "Densidad:" & Chr(13) & Chr(10) & "Ph:" & Chr(13) & Chr(10) & "SO:" & Chr(13) & Chr(10) & "Proteinas:" & Chr(13) & Chr(10) & "CE
:" & Chr(13) & Chr(10) & "LL:" & Chr(13) & Chr(10) & "FM:" & Chr(13) & Chr(10) & "Nitrito:" & Chr(13) & Chr(10) & "Cristales:",[Examenes]="BHC, EGH, EGO, VDRL","BHC" & Chr(13) & Chr(10) & "Hto:" & Chr(13) & Chr(10) & "Gb:" & Chr(13) & Chr(10) & "E:" & Chr(13) & Chr(10) & "S:" & Chr(13) & Chr(10) & "L:" & Chr(13) & Chr(10) & "M:" & Chr(13) & Chr(10) & "St:" & Chr(13) & Chr(10) & "B:" & Chr(13) & Chr(10) & "VDRL:" & Chr(13) & Chr(10) & "EGO" & Chr(13) & Chr(10) & "Color:" & Chr(13) & Chr(10) & "Densidad:" & Chr(13) & Chr(10) & "Ph:" & Chr(13) & Chr(10) & "SO:" & Chr(13) & Chr(10) & "Proteinas:" & Chr(13) & Chr(10) & "CE:" & Chr(13) & Chr(10) & "LL:" & Chr(13) & Chr(10) & "FM:" & Chr(13) & Chr(10) & "Nitrito:" & Chr(13) & Chr(10) & "Cristales:" & Chr(13) & Chr(10) & "EGH" & Chr(13) & Chr(10) & "Protozoarios:" & Chr(13) & Chr(10) & "Metazoarios:")
End Sub

非常感谢任何帮助!

【问题讨论】:

【参考方案1】:

尝试使用简单的Select ... Case 构造:

Private Sub Report_Open(Cancel As Integer)

    Const BHC   As String = "BHC" & vbCrLf & "Hto:" & vbCrLf & "Gb:" & vbCrLf & "E:" & vbCrLf & "S:" & vbCrLf & "L:" & vbCrLf & "M:" & vbCrLf & "St:" & vbCrLf & "B:"
    Const VDRL  As String = vbCrLf & "VDRL:"    
    Const EGO   As String = vbCrLf & "EGO" & vbCrLf & "Color:" & vbCrLf & "Densidad:" & vbCrLf & "Ph:" & vbCrLf & "SO:" & vbCrLf & "Proteinas:" & vbCrLf & "CE:" & vbCrLf & "LL:" & vbCrLf & "FM:" & vbCrLf & "Nitrito:" & vbCrLf & "Cristales:"
    Const EGH   As String = vbCrLf & "EGH" & vbCrLf & "Protozoarios:" & vbCrLf & "Metazoarios:"

    Dim Value   As string

    Select Case Me!Examenes.Value
        Case "BHC"
            Value = BHC
        Case "BHC, VDRL"
            Value = BHC & VDRL
        Case "BHC, EGO, VDRL"
            Value = BHC & VDRL & EGO
        Case "BHC, EGH, EGO, VDRL"
            Value = BHC & VDRL & EGO & EGH
    End Select

    ' Display result.
    Me!YourUnboundTextbox.Value = Value

End Sub

【讨论】:

太棒了!!!非常感谢古斯塔夫!!!我会尽快尝试您提出的解决方案! 你好@Gustav!我一直在研究您推荐的方法,但在让我的未绑定文本框显示变量时遇到问题。这是代码:Private Sub Orden_Open(Cancel As Integer) Const EGH As String = "EGH" & vbCrLf & "Protozoarios:" & vbCrLf & "Metazoarios:" Const VDRL As String = "VDRL:" Dim ListOrder As String Select Case Me!Examenes.Value Case "VDRL" ListOrder = VDRL Case "EGH" ListOrder = EGH End Select Me!txtList.Value = ListOrder End Sub 非常感谢您的支持 不确定Orden_Open 可能是什么。使用我的代码,在最后一行替换你的文本框的名称。 再次感谢... Orden_Open 是我的报告名称和列表。我会再做一次。再次感谢。 你好@Gustav。我对第 1 行代码中的 Orden_Open 部分完全错误!我的印象是我必须将报告的名称放在_Open 后面。我纠正了我的错误并将其保留为“Report_Open”。但是,我收到运行时错误 2427:“您输入了一个没有值的表达式”,并使用代码 Select Case Me!txtExamenes.Value 突出显示该行。如果我删除整个代码并打开报告,我会看到 txtExamenes 文本框返回存储在表中的值,所以我不知道为什么会出现运行时错误。请帮我解决这个问题?

以上是关于如何在访问报告中生成列表?的主要内容,如果未能解决你的问题,请参考以下文章

如何在列表中生成动态 url?

如何在 HSQLDB 中生成一个范围内的数字行列表?

如何在 Django REST Swagger 中生成响应消息列表?

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

如何在 Java 中生成顺序整数的列表或数组?

如何有效地查询多个表以在 dataGrid 中生成 excel STYLE 报告