Access 中的窗體和數(shù)據(jù)表支持兩種新視圖:PivotTable®(數(shù)據(jù)透視表)視圖和 PivotChart®(數(shù)據(jù)透視圖)視圖。數(shù)據(jù)透視表視圖使用 Office 數(shù)據(jù)透視表組件,易于進(jìn)行交互式數(shù)據(jù)分析。數(shù)據(jù)透視圖視圖使用 Office Chart 組件,幫助您創(chuàng)建動(dòng)態(tài)的交互式圖表。這些視圖支持交互操作,例如添加、篩選和排序數(shù)據(jù),而無需寫入代碼。然而,如果要生成基于用戶輸入的運(yùn)行時(shí)視圖,則需要編寫代碼。本文介紹了在數(shù)據(jù)透視表和數(shù)據(jù)透視圖視圖中執(zhí)行簡單任務(wù)(例如移動(dòng)和篩選字段)的示例代碼。
有關(guān)向窗體添加代碼的詳細(xì)信息,請參閱 Access 幫助。
注釋 下面的示例基于 Access 包含的羅斯文貿(mào)易示例數(shù)據(jù)庫。
包含對 Office Web 組件庫的引用
要運(yùn)行下面的示例代碼,您的數(shù)據(jù)庫必須包含對 Owc11.dll(對于 Access 2003)或 Owc10.dll(對于 Access 2002)的引用。使用 Access 2000 或更早版本創(chuàng)建的數(shù)據(jù)庫,或使用 Access 2002 或更高版本創(chuàng)建但以 Access 2000 文件格式保存的數(shù)據(jù)庫,不包括此引用。
將對 Owc11.dll 或 Owd10.dll 的引用添加至 Access 數(shù)據(jù)庫
打開準(zhǔn)備添加引用的數(shù)據(jù)庫。
在工具欄上單擊“代碼”,打開 Microsoft Visual Basic® 編輯器。
在“工具”菜單上,單擊“引用”。
單擊“瀏覽”并導(dǎo)航到 Owc11.dll 或 Owc10.dll 文件所在的位置。
對于 Access 2003,該文件的默認(rèn)位置是 C:/Program Files/Common Files/Microsoft Shared/Web Components/11;對于 Access 2002,默認(rèn)位置是 C:/Program Files/Common Files/Microsoft Shared/Web Components/10。
選擇該文件,單擊“打開”,然后單擊“確定”。
“數(shù)據(jù)透視表”視圖示例
更改視圖的布局
向行區(qū)域、列區(qū)域和明細(xì)區(qū)域添加字段
下面的代碼將 CustomerID、ShipVia 和 Freight 字段添加至窗體(基于“Orders”表)“數(shù)據(jù)透視表”視圖的行、列和明細(xì)區(qū)域。
Dim fset1, fset2, fset3 As PivotFieldSet
Set fset1 = Me.PivotTable.ActiveView.FieldSets("CustomerID")
Set fset2 = Me.PivotTable.ActiveView.FieldSets("ShipVia")
Set fset3 = Me.PivotTable.ActiveView.FieldSets("Freight")
Me.PivotTable.ActiveView.RowAxis.InsertFieldSet fset1
Me.PivotTable.ActiveView.ColumnAxis.InsertFieldSet fset2
Me.PivotTable.ActiveView.DataAxis.InsertFieldSet fset3
添加總計(jì)字段
下面的代碼創(chuàng)建了用于計(jì)算 CustomerID 數(shù)目的匯總字段,并將該字段添加至視圖的明細(xì)區(qū)域。
Me.PivotTable.ActiveView.AddTotal "Count Of Customers", _
Me.PivotTable.ActiveView.FieldSets("CustomerID").Fields("CustomerID"), _
plFunctionCount
Me.PivotTable.ActiveView.DataAxis.InsertTotal _
Me.PivotTable.ActiveView.Totals("Count Of Customers")
有關(guān)向窗體添加代碼的詳細(xì)信息,請參閱 Access 幫助。
注釋 下面的示例基于 Access 包含的羅斯文貿(mào)易示例數(shù)據(jù)庫。
包含對 Office Web 組件庫的引用
要運(yùn)行下面的示例代碼,您的數(shù)據(jù)庫必須包含對 Owc11.dll(對于 Access 2003)或 Owc10.dll(對于 Access 2002)的引用。使用 Access 2000 或更早版本創(chuàng)建的數(shù)據(jù)庫,或使用 Access 2002 或更高版本創(chuàng)建但以 Access 2000 文件格式保存的數(shù)據(jù)庫,不包括此引用。
將對 Owc11.dll 或 Owd10.dll 的引用添加至 Access 數(shù)據(jù)庫
打開準(zhǔn)備添加引用的數(shù)據(jù)庫。
在工具欄上單擊“代碼”,打開 Microsoft Visual Basic® 編輯器。
在“工具”菜單上,單擊“引用”。
單擊“瀏覽”并導(dǎo)航到 Owc11.dll 或 Owc10.dll 文件所在的位置。
對于 Access 2003,該文件的默認(rèn)位置是 C:/Program Files/Common Files/Microsoft Shared/Web Components/11;對于 Access 2002,默認(rèn)位置是 C:/Program Files/Common Files/Microsoft Shared/Web Components/10。
選擇該文件,單擊“打開”,然后單擊“確定”。
“數(shù)據(jù)透視表”視圖示例
更改視圖的布局
向行區(qū)域、列區(qū)域和明細(xì)區(qū)域添加字段
下面的代碼將 CustomerID、ShipVia 和 Freight 字段添加至窗體(基于“Orders”表)“數(shù)據(jù)透視表”視圖的行、列和明細(xì)區(qū)域。
Dim fset1, fset2, fset3 As PivotFieldSet
Set fset1 = Me.PivotTable.ActiveView.FieldSets("CustomerID")
Set fset2 = Me.PivotTable.ActiveView.FieldSets("ShipVia")
Set fset3 = Me.PivotTable.ActiveView.FieldSets("Freight")
Me.PivotTable.ActiveView.RowAxis.InsertFieldSet fset1
Me.PivotTable.ActiveView.ColumnAxis.InsertFieldSet fset2
Me.PivotTable.ActiveView.DataAxis.InsertFieldSet fset3
添加總計(jì)字段
下面的代碼創(chuàng)建了用于計(jì)算 CustomerID 數(shù)目的匯總字段,并將該字段添加至視圖的明細(xì)區(qū)域。
Me.PivotTable.ActiveView.AddTotal "Count Of Customers", _
Me.PivotTable.ActiveView.FieldSets("CustomerID").Fields("CustomerID"), _
plFunctionCount
Me.PivotTable.ActiveView.DataAxis.InsertTotal _
Me.PivotTable.ActiveView.Totals("Count Of Customers")