閱讀英文

共用方式為


如何以程序設計方式在 Access 報表中建立新的數據行

原始 KB 編號: 812719

摘要

本文說明如何以程序設計方式在 Access 報表中建立數據行。 您可以使用一節中所述的方法 1 或方法 2,動態地將數據行新增至報表。

注意

本文中的範例程式代碼使用 Microsoft 資料存取物件。 若要讓此程式代碼正常執行,您必須參考 Microsoft DAO 3.6 物件庫。 若要這樣做,請按兩下Visual Basic 編輯器 中 [工具] 選單上的 [參考],並確定已選取 [Microsoft DAO 3.6 物件庫] 複選框。

方法 1:以程式設計方式將數據行新增至報表

下列範例示範如何以程序設計方式建立 Access 報表。 程式代碼會產生以記錄來源查詢為基礎的報表。 產生的報表會顯示 Northwind.mdb 範例資料庫之 Employees 數據表的 Firstname 數據行和 Lastname 數據行。

  1. 啟動存取。

  2. 在 [ 說明] 功能表上,單擊 [ 範例資料庫],然後按兩下 [Northwind 範例資料庫]。當 主切換板 表單出現時,請關閉該表單。

    注意

    在 Access 2007 中,單擊 [範例類別目錄] 窗格中的 [範],按兩下 [Northwind 2007],然後按兩下 [下載]

  3. 在左窗格中,按兩下 [ 報表]

    注意

    在 Access 2007 中,略過此步驟。

  4. 在右窗格中,按兩下 [ 設計檢視] 中的 [建立報表]

    注意

    在 Access 2007 中,按兩下 [建立] 索引標籤上 [表] 群組中的 [報表設計]

  5. 在 [ 檔案] 功能表上,按兩下 [ 儲存]

    注意

    在 Access 2007 中,按兩下 [Microsoft Office 按鈕],然後按兩下 [ 儲存]

  6. 在 [ 另存新 檔] 對話框中,輸入 AccessColumnBuilder,然後按兩下 [ 確定]

  7. 關閉報表。

  8. 在左窗格中,選取 [ 表單]

    注意

    在 Access 2007 中,略過此步驟。

  9. 在右窗格中,按兩下 [ 設計檢視] 中的 [建立表單]

    注意

    在 Access 2007 中,按兩下 [建立] 索引標籤上 [單] 群組中的 [表單設計]

  10. 將命令按鈕新增至表單。

    注意

    在 Access 2007 中,按兩下 [按鈕] 將按鈕新增至 [設計] 索引卷標上 [控制項] 群組中的表單。

  11. 以滑鼠右鍵按下命令按鈕,按兩下 [建置事件],按兩下 [選擇產生器] 對話框中的 [程式代碼產生器],然後按兩下 [確定]

  12. 將下列程式代碼新增至命令按鈕的 OnClick 事件:

    VB
    Dim txtNew As Access.TextBox
    Dim labNew As Access.Label
    Dim lngTop      As Long
    Dim lngLeft     As Long
    Dim lblCol    As  Long
    Dim rpt As Report
    Dim reportQuery As String
    Dim rs As DAO.Recordset
    Dim i As Integer
    Dim prevColwidth As long
    
    lngLeft = 0
    lngTop = 0
    
    ' Open the report to design.
    ' To make changes in the number of columns that appear at run time.
    
    DoCmd.OpenReport "AccessColumnBuilder", acViewDesign
    
    Set rpt = Reports![AccessColumnBuilder]
    
    ' Change the number of columns required as per your requirement.
    reportQuery = "SELECT FirstName, LastName FROM Employees"
    
    ' Open the recordset.
    Set rs = CodeDb().OpenRecordset(reportQuery)
    ' Assign the query as a record source to report control.
    rpt.RecordSource = reportQuery
    
    ' Set the value to zero so that the left margin is initialized.
    prevColwidth = 0
    lblCol = 0
    ' Print the page header for the report.
    For i = 0 To rs.Fields.Count - 1
    Set labNew = CreateReportControl(rpt.Name, acLabel, acPageHeader, _
      , rs.Fields(i).Name, lblcol, , , lngTop)
      labNew.SizeToFit
      lblCol = lblCol + 600 + labNew.Width
    Next
    
    ' Create the column depending on the number of fields selected in reportQuery.
    ' Assign the column value to new created column.
    For i = 0 To rs.Fields.Count - 1
      ' Create new text box control and size to fit data.
      Set txtNew = CreateReportControl(rpt.Name, acTextBox, _
         acDetail, , , lngLeft + 15 + prevColwidth, lngTop)
      txtNew.SizeToFit
      txtNew.ControlSource = rs(i).Name
      ' Modify the left margin depending on the number of columns
      ' and the size of each column.
      prevColwidth = prevColwidth + txtNew.width
    Next
    'To save the modification to the report,  uncomment the following line of code:
    'DoCmd.Save
    ' View the generated report.
    DoCmd.OpenReport "AccessColumnBuilder", acViewPreview
    
    ' This opens the report in preview.
    
  13. 儲存然後執行表單。

  14. 若要預覽報表,請按下您在步驟 10 中新增的命令按鈕。

    下列記錄會出現在第一頁:

    主控台
    First NameLast Name
    
    NancyDavolio
    
    MargaretPeacock
    
    ...............
    

    報表包含 Employees 數據表的Firstname 資料行和 Lastname 資料行。 您可以儲存報表,或對查詢進行必要的變更,然後執行報表。

方法 2:藉由設定現有資料行的可見屬性,在運行時間將數據行新增至報表

此範例示範如何藉由操作 控件的 Visible 屬性,在報表中顯示新的數據行。

報表包含四個數據行。 Visible前三個數據行的 屬性設定為 [是]。 第 Visible 四個數據行的 屬性設定為 no。 在分頁符號上 Visible ,當數據行出現時,第四個數據行的 屬性會設定為 [是 ]。

  1. 啟動存取。

  2. 在 [ 說明] 功能表上,單擊 [ 範例資料庫],然後按兩下 [Northwind 範例資料庫]。 當 主切換板 表單出現時,請關閉該表單。

    注意

    在 Access 2007 中,單擊 [範例類別目錄] 窗格中的 [範],按兩下 [Northwind 2007],然後按兩下 [下載]

  3. 若要建立名為 Report1 且以 Products 數據表為基礎的報表,請遵循下列步驟:

    1. 在 [ 資料庫] 視窗中,按兩下 [報告 ],然後按下 [ 新增]

      注意

      在 Access 2007 中,按兩下 [建立] 索引標籤上 [表] 群組中的 [表精靈]

    2. 在 [ 新增報表] 對話框中,按兩下 [ 設計視圖],選取 [ 產品],然後按兩下 [ 確定]

    3. 將下列文字框新增至報表的 [詳細 數據] 區段。 對齊文本框。

    4. 將對應的標籤放在報表的 [頁首] 區段中。 對齊標籤。

      主控台
      Report: Report1
      --------------------------
      Caption: TestReport
      ControlSource: Products
      
      Label:
         Name: ProductName_label
      
      Text Box:
         Name: ProductName
         ControlSource: ProductName
      
      Label:
         Name: UnitPrice_label
      
      Text Box:
         Name: UnitPrice
         ControlSource: UnitPrice
      
      Label:
         Name: UnitsInStock_label
      
      Text Box:
         Name: UnitsInStock
         ControlSource: UnitsInStock
      
      Label:
         Name: TotalPrice_label
         Visible: No
      
      Text Box:
         Name:  TotalPrice
         ControlSource:  =[UnitPrice] * [UnitsInStock]
         Visible: No
      
  4. 將具有下列屬性的文字框控制項新增至 [詳細 數據] 區段。 將文本框放在 ProductName 控制件的正上方。

    此控件會作為報表中記錄數目的計數器。

    主控台
    Text Box:
    -----------------------
    Name: Counter
    ControlSource: =1
    Visible: No
    RunningSum: Over All
    
  5. 在 [ 工具箱] 上,按兩下 [ 分頁符號]

    注意

    在 Access 2007 中,按兩下 [設計] 索引標籤上 [控件] 群組中的 [新增或移除分頁符號]

  6. 將分頁符號控件新增至 [ 詳細 數據] 區段的左下角。 將分頁符號控件放在 ProductName 控件正下方。 將 Name 屬性設定為 PageBreak

  7. 在 [ 詳細數據] 區段中,將 OnFormat 屬性設定為下列事件過程:

    主控台
    If Me![Counter] Mod 2 = 0 Then Me![PageBreak].Visible = True _
    Else Me![PageBreak].Visible = False
    
  8. 若要減少報表中的空白空間,請將您的指標放在 [ 詳細 數據] 區段的底部和 [頁尾 ] 之間,然後向上拖曳。

  9. 在 [ 頁尾] 區 段中,將 OnPrint 屬性設定為下列事件過程:

    主控台
    If Me![PageBreak].Visible = True Then
     Me![TotalPrice].Visible = True
     Me![TotalPrice_label].Visible = True
    Else
     Me![TotalPrice].Visible = False
     Me![TotalPrice_label].Visible = False
    End If
    

    發生 PageBreak 時,[TotalPrice] 數據行隨即出現。

  10. 預覽報表。 下列記錄會出現在第一頁:

    主控台
    Product NameUnit PriceUnit in Stock
    
    Cahi$18.0039
    
    Chang$19.0017
    

    注意

    報表的第一頁包含三個數據行。

    下列記錄會出現在第二頁:

    主控台
    Product NameUnit PriceUnit in StockTotal Amount
    
    Aniseed Syrup $10.0013    130
    
    Chef A...    $22.00 53    1166
    

    注意

    分頁符號會在第一頁之後發生。 因此,報表的第二頁包含四個數據行。

參考資料

如需如何以程序設計方式建立 Access 報告的詳細資訊,請流覽下列文章:

建立簡單的報表