CurrentProject 對象引用當前 Microsoft Access 項目(.adp)或 Access 數(shù)據(jù)庫(.mdb)的項目。
CurrentProject 對象有好幾個集合,每個集合都包含當前數(shù)據(jù)庫中的特定 AccessObject 對象。下表列出了每個集合的名稱及其所含對象的類型。
集合 |
對象類型 |
所有窗體 |
|
所有報表 |
|
所有宏 |
|
所有模塊 |
|
所有數(shù)據(jù)訪問頁 |
注釋 上表中的集合包含了數(shù)據(jù)庫中各方面的對象,不管它們是打開還是關閉。
例如,代表窗體的 AccessObject 對象是 AllForms 集合的一個成員,而 AllForms 集合是當前數(shù)據(jù)庫中 AccessObject 對象的一個集合。在 AllForms 集合中,集合的每個成員從零開始編制索引。通過按名稱引用窗體或引用其在集合中的索引,可以引用 AllForms 集合中的單個 AccessObject 對象。如果想引用 AllForms 集合中的特定對象,最好按名稱引用它,因為項的集合索引可能會變動。如果對象名中包含空格,則必須將名稱用方括號([ ])括起來。
語法 |
示例 |
AllForms!formname |
AllForms!OrderForm |
AllForms![form name] |
AllForms![Order Form] |
AllForms("formname") |
AllForms("OrderForm") |
AllForms(index) |
AllForms(0) |
下面的示例打印 CurrentProject 對象的一些當前屬性設置,然后設置選項以顯示應用程序中的隱藏對象:
Sub ApplicationInformation()
' Print name and type of current object.
Debug.Print Application.CurrentProject.FullName
Debug.Print Application.CurrentProject.ProjectType
' Set Hidden Objects option under Show on View Tab
'of the Options dialog box.
Application.SetOption "Show Hidden Objects", True
End Sub
下一個示例顯示如何使用“自動化”從另一個 Microsoft Office 應用程序中使用 CurrentProject 對象。首先,從另一個應用程序中創(chuàng)建對 Microsoft Access 的引用,方法是:在“模塊”窗口中單擊“工具”菜單上的“引用”。選中 Microsoft Access Object Library 旁的復選框。然后在該應用程序的 Visual Basic 模塊中輸入下列代碼,并調(diào)用 GetAccessData 過程。
該示例將一個數(shù)據(jù)庫名和報表名傳遞給一個創(chuàng)建 Application 類的新實例的過程,打開數(shù)據(jù)庫,并使用 CurrentProject 對象和 AllReports 集合驗證指定的報表是否存在。
Sub GetAccessData()
' Declare object variable in declarations section of a module
Dim appAccess As Access.Application
Dim strDB As String
Dim strReportName As String
strDB = "C:\Program Files\Microsoft "_
& "Office\Office11\Samples\Northwind.mdb"
strReportName = InputBox("Enter name of report to be verified", _
"Report Verification")
VerifyAccessReport strDB, strReportName
End Sub
Sub VerifyAccessReport(strDB As String, _
strReportName As String)
' Return reference to Microsoft Access
' Application object.
Set appAccess = New Access.Application
' Open database in Microsoft Access.
appAccess.OpenCurrentDatabase strDB
' Verify report exists.
On Error Goto ErrorHandler
appAccess.CurrentProject.AllReports(strReportName)
MsgBox "Report " & strReportName & _
" verified within Northwind database."
appAccess.CloseCurrentDatabase
Set appAccess = Nothing
Exit Sub
ErrorHandler:
MsgBox "Report " & strReportName & _
" does not exist within Northwind database."
appAccess.CloseCurrentDatabase
Set appAccess = Nothing
End Sub