OpenCurrentDatabase 方法

此頁(yè)沒有內(nèi)容條目
內(nèi)容

expandtri全部顯示

使用 OpenCurrentDatabase 方法可以打開一個(gè)已有 Microsoft Access 數(shù)據(jù)庫(kù) (.mdb) 作為當(dāng)前的數(shù)據(jù)庫(kù)。

expression.OpenCurrentDatabase(filepath, Exclusive, bstrPassword)

expression     必需。返回“應(yīng)用于”列表中的一個(gè)對(duì)象的表達(dá)式。

filepath     必需 String 型。字符串表達(dá)式,表示已有數(shù)據(jù)庫(kù)文件的名稱,包括路徑名和文件名擴(kuò)展。如果網(wǎng)絡(luò)支持,也可以按以下形式指定網(wǎng)絡(luò)路徑:\\Server\Share\Folder\Filename

注釋   如果沒有提供文件名擴(kuò)展名,則將 .mdb 追加到文件名后。

Exclusive     可選 Boolean 值。指定是否以獨(dú)占 方式打開數(shù)據(jù)庫(kù)。默認(rèn)值是 False,該值指定以共享方式打開數(shù)據(jù)庫(kù)。

bstrPassword     可選 String。打開指定數(shù)據(jù)庫(kù)的密碼。

說(shuō)明

使用該方法可以從另一個(gè)應(yīng)用程序中打開數(shù)據(jù)庫(kù),該應(yīng)用程序通過(guò)自動(dòng)化(以前稱為 OLE 自動(dòng)化)控制 Microsoft Access。例如,可以使用 OpenCurrentDatabase 方法從 Microsoft Excel 中打開 Microsoft Access 窗口中的羅斯文示例數(shù)據(jù)庫(kù)。一旦通過(guò)另一個(gè)應(yīng)用程序創(chuàng)建了 Microsoft Access 實(shí)例,則也必需創(chuàng)建新的數(shù)據(jù)庫(kù)或指定要打開的特殊數(shù)據(jù)庫(kù)。該數(shù)據(jù)庫(kù)是在 Microsoft Access 窗口中打開的。

注釋  使用 OpenAccessProject 方法打開一個(gè)已有 Microsoft Access 項(xiàng)目 (.adp) 作為當(dāng)前的數(shù)據(jù)庫(kù)。

在 Microsoft Access 窗口中,如果已經(jīng)打開一個(gè)數(shù)據(jù)庫(kù)又希望打開另一個(gè)數(shù)據(jù)庫(kù),在打開另一個(gè)數(shù)據(jù)庫(kù)以前,可以使用 CloseCurrentDatabase 方法關(guān)閉第一個(gè)數(shù)據(jù)庫(kù)。

Exclusive 參數(shù)設(shè)為 True 可以以獨(dú)占方式打開數(shù)據(jù)庫(kù)。如果忽略該參數(shù),數(shù)據(jù)庫(kù)將以共享方式打開。

注釋  請(qǐng)不要將 OpenCurrentDatabase 方法與 ActiveX 數(shù)據(jù)對(duì)象 (ADO) 的 Open 方法或數(shù)據(jù)訪問(wèn)對(duì)象 (DAO) 的 OpenDatabase 方法混淆。OpenCurrentDatabase 方法在 Microsoft Access 窗口中打開數(shù)據(jù)庫(kù)。ADO 的 Open 方法返回 Connection 對(duì)象變量,而 DAO 的 OpenDatabase 方法返回 Database 對(duì)象變量。兩者均代表一個(gè)特定的數(shù)據(jù)庫(kù),但都不在 Microsoft Access 窗口中實(shí)際打開數(shù)據(jù)庫(kù)。

示例

下面的示例通過(guò)“自動(dòng)化”從其他應(yīng)用程序中打開一個(gè) Microsoft Access 數(shù)據(jù)庫(kù),然后打開一個(gè)數(shù)據(jù)庫(kù)中的窗體。

可以在任何一個(gè)可以用作 COM 組件的應(yīng)用程序的 Visual Basic 模塊中輸入這段代碼。例如,可以從 Microsoft Excel、Microsoft Visual Basic 或 Microsoft Access 中運(yùn)行下列代碼。

當(dāng)指向 Application 對(duì)象的變量超出范圍時(shí),它所表示的 Microsoft Access 實(shí)例也將關(guān)閉。所以,必須在模塊級(jí)聲明這個(gè)變量。

' Include the following in Declarations section of module.

Dim appAccess As Access.Application

Sub DisplayForm()

    Dim strDB as String

    ' Initialize string to database path.

    Const strConPathToSamples = "C:\Program " _

        & "Files\Microsoft Office\Office11\Samples\"

    strDB = strConPathToSamples & "Northwind.mdb"

    ' Create new instance of Microsoft Access.

    Set appAccess = _

        CreateObject("Access.Application")

    ' Open database in Microsoft Access window.

    appAccess.OpenCurrentDatabase strDB

    ' Open Orders form.

    appAccess.DoCmd.OpenForm "Orders"

End Sub