2014-08-29 110 views
1

我在大約10年內沒有使用Access,需要爲1次問題做一個簡單的報告。我有一份顯示員工工作歷史的報告。它已經很好地按名稱分組了,但是我想要做的是在滿足搜索條件的名稱完整報告上面的摘要。我怎麼會在訪問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) 

回答

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 
+0

我嘗試添加一個子查詢簡單地用於填充報告的其他部分查詢選擇名稱列,但後來我最終進入搜索標準兩次。 – Travis 2014-08-29 12:05:16

+0

您應該能夠使用表格的「Master-Child」屬性鏈接報告以避免這種情況。 – ashareef 2014-08-29 12:17:14

+0

或者您沒有正確引用表單上的字段(我假設的搜索條件?)。 – 2014-08-29 12:18:56