Access报告中的结果摘要

时间:2014-08-29 11:57:18

标签: ms-access ms-access-2007

我在大约10年内没有使用过Access,需要针对一次性问题做一个简单的报告。我有一份报告显示员工'工作经历。它已按名称很好地分组,但我想要做的是在满足搜索条件的完整报告名称之上的摘要。我将如何在Access 2k7中执行此操作

-------------------------  | 
These employees were found |
Bob                        |
Joe                        |-Part I would like to add to the report
Steve                      |
Alan                       |
-------------------------  |
Name:
Bob
-------------------------
Work history 1
Work History 2
Work history (n)

Name:
Joe
-------------------------
Work history 1
Work History 2
Work history (n)

1 个答案:

答案 0 :(得分:2)

您可以创建一个自定义VBA函数,该函数循环遍历记录集并构建连接字符串以进行输出。下面是一个打开报告的按钮,阅读一般策略的评论

Private Sub OpenReport_Click()
    Dim searchCriteria As String
    Dim sql As String
    searchCriteria = InputBox("Input search criteria here")
    ' you can do some validation on the search criteria here if you wish

    ' creating the sql string for the record source of the report
    sql = "SELECT * FROM employee_workhistory WHERE workhistory = '" & searchCriteria & "'"

    ' opening report with blank record source
    DoCmd.OpenReport "workhistoryReport", acViewReport
    With Reports.Item("workhistoryReport")
        .RecordSource = sql
        ' assigning the control sources of the textboxes in report shouldn't be necesarry but report didn't
        ' seem to "refresh" until a control was explicitly assigned even if it is the exact same control source
        .Controls("employee").ControlSource = "employee"
        .Controls("workhistory").ControlSource = "workhistory"
    End With

    ' looping through a group by query with results of the same search criteria
    Dim rs As Recordset
    sql = "SELECT employee FROM employee_workhistory WHERE workhistory = '" & searchCriteria & "' GROUP BY employee"
    Set rs = CurrentDb.OpenRecordset(sql, dbOpenDynaset)
    Do Until rs.EOF
        ' build the string you want to place on report here and assign to a text box
        ' in header of the report
        Debug.Print rs("employee")
        rs.MoveNext
    Loop
End Sub