閱讀英文

共用方式為


如何在 Access 資料庫中強制執行或停用啟動選項

本文僅適用於 microsoft Access 專案 (.adp) 。

中等:需要基本宏、編碼和互操作性技能。

摘要

本文說明如何停用 SHIFT 鍵的功能,讓您略過啟動選項。 本文也說明如何在 Microsoft Access 資料庫專案中強制執行啟動選項。

其他相關資訊

針對 Access 檔案定義的啟動選項會決定檔案的外觀,以及當您開啟檔案時檔案的行為。 您可以使用啟動使用者介面或使用 AutoExec 宏來設定啟動選項。

若要略過為 Access 資料庫項目設定的啟動選項,請在開啟 Access 資料庫專案時按住 SHIFT 鍵。

或者,若要強制執行為 Access 資料庫專案設定的啟動選項,請停用 SHIFT 鍵的功能,讓您略過啟動選項。 若要這樣做,請將AllowBypassKey屬性設定為 False。

若要將AllowBypassKey屬性設定為 False,請遵循下列步驟。

Access 專案的步驟 (.adp)

  1. 啟動存取。

  2. 開啟 Access 資料庫專案。

  3. 按 ALT + F11 以開啟 Visual Basic 編輯器。

  4. 在 Visual Basic 編輯器中,單擊 [檢視] 功能表上的 [即時運算視窗]。

  5. 輸入下列程式代碼,或在 [即時運算] 視窗中貼上下列程式代碼,然後按 ENTER。

    VB
    CurrentProject.Properties.Add "AllowBypassKey", False
    
  6. 關閉 Visual Basic 編輯器,然後關閉 Access 資料庫專案。

  7. 開啟 Access 資料庫專案。 開啟 Access 資料庫專案時按住 SHIFT 鍵,嘗試略過為 Access 資料庫專案設定的啟動選項。

    允許您略過啟動選項的 SHIFT 鍵功能已停用。 雖然您按住 SHIFT 鍵略過啟動選項,但會執行啟動選項。 您無法略過啟動選項。

Access 資料庫 (.mdb 或 .accdb) 的步驟

  1. 啟動存取。

  2. 建立新的模組,然後新增下列兩個函式:

    VB
    Function ap_DisableShift()
    'This function disable the shift at startup. This action causes
    'the Autoexec macro and Startup properties to always be executed.
    
    On Error GoTo errDisableShift
    
    Dim db As DAO.Database
    Dim prop as DAO.Property
    Const conPropNotFound = 3270
    
    Set db = CurrentDb()
    
    'This next line disables the shift key on startup.
    db.Properties("AllowByPassKey") = False
    
    'The function is successful.
    Exit Function
    
    errDisableShift:
    'The first part of this error routine creates the "AllowByPassKey
    'property if it does not exist.
    If Err = conPropNotFound Then
    Set prop = db.CreateProperty("AllowByPassKey", _
    dbBoolean, False)
    db.Properties.Append prop
    Resume Next
    Else
    MsgBox "Function 'ap_DisableShift' did not complete successfully."
    Exit Function
    End If
    
    End Function
    
    Function ap_EnableShift()
    'This function enables the SHIFT key at startup. This action causes
    'the Autoexec macro and the Startup properties to be bypassed
    'if the user holds down the SHIFT key when the user opens the database.
    
    On Error GoTo errEnableShift
    
    Dim db as DAO.Database
    Dim prop as DAO.Property
    Const conPropNotFound = 3270
    
    Set db = CurrentDb()
    
    'This next line of code disables the SHIFT key on startup.
    db.Properties("AllowByPassKey") = True
    
    'function successful
    Exit Function
    
    errEnableShift:
    'The first part of this error routine creates the "AllowByPassKey
    'property if it does not exist.
    If Err = conPropNotFound Then
    Set prop = db.CreateProperty("AllowByPassKey", _
    dbBoolean, True)
    db.Properties.Append prop
    Resume Next
    Else
    MsgBox "Function 'ap_DisableShift' did not complete successfully."
    Exit Function
    End If
    
    End Function
    
  3. 在 Visual Basic 編輯器中,單擊 [檢視] 功能表上的 [即時運算視窗]。

  4. 如果您想要停用 SHIFT 鍵,請在 [ 即時 運算] 視窗中輸入 ap_DisableShift,然後按 ENTER 鍵。 如果您想要啟用移位鍵,請在 [ 即時 運算] 視窗中輸入 ap_EnableShift,然後按 ENTER 鍵。