CurrentProject 物件 (Access)

CurrentProject 物件參照目前 Microsoft Access 專案 (.adp) 或 Access 資料庫中的專案。

註解

CurrentProject物件有數個集合,其中包含目前資料庫內的特定AccessObject物件。 下表列出了每個集合的名稱,以及其中包含的物件類型。

集合 物件類型
AllForms 所有表單
AllReports 所有報表
AllMacros 所有巨集
AllModules 所有模組

注意事項

[!注意事項] 上表中的集合包含了資料庫中各方面的物件,不管它們是否開啟還是關閉。

例如,代表在表單的 AccessObject 物件是 AllForms 集合,這是目前資料庫中的 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 物件。 首先,從另一個應用程式,選擇 [模組] 視窗中 [工具] 功能表上的 [參考],以建立 Microsoft Access 的參考。 選取 Microsoft Access 物件程式庫旁邊的核取方塊,然後在該應用程式內的 Visual Basic 模組中輸入下列程式碼,然後呼叫 GetAccessData 程式。

此範例會將資料庫名稱和報表名稱傳遞給程式,以建立 Application 類別的新 實例、開啟資料庫,並使用 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 
 IsObject appAccess.CurrentProject.AllReports(strReportName) 
 MsgBox "Report " & strReportName & _ 
        " verified within " & appAccess.CurrentProject.Name & " database."
 appAccess.CloseCurrentDatabase 
 Set appAccess = Nothing 
Exit Sub 
ErrorHandler: 
 MsgBox "Report " & strReportName & _ 
        " does not exist within " & appAccess.CurrentProject.Name & " database."
 appAccess.CloseCurrentDatabase 
 Set appAccess = Nothing 
End Sub

方法

屬性

請參閱

支援和意見反應

有關於 Office VBA 或這份文件的問題或意見反應嗎? 如需取得支援服務並提供意見反應的相關指導,請參閱 Office VBA 支援與意見反應