如何在 Access 数据库中强制或禁用启动选项

本文仅适用于 Microsoft Access 项目 (.adp) 。

中等:需要基本的宏、编码和互操作性技能。

摘要

本文介绍如何禁用允许绕过启动选项的 SHIFT 键的功能。 本文还介绍如何在 Microsoft Access 数据库项目中强制实施启动选项。

更多信息

为 Access 文件定义的启动选项确定文件的外观以及打开文件时文件的行为方式。 可以使用启动用户界面或使用 AutoExec 宏设置启动选项。

若要绕过为 Access 数据库项目设置的启动选项,请在打开 Access 数据库项目时按住 Shift 键。

或者,若要强制实施为 Access 数据库项目设置的启动选项,请禁用允许绕过启动选项的 SHIFT 键的功能。 为此,请将 AllowBypassKey 属性设置为 False。

若要将 AllowBypassKey 属性设置为 False,请执行以下步骤。

Access 项目 (.adp) 的步骤

  1. 启动 Access。

  2. 打开 Access 数据库项目。

  3. 按 Alt + F11 打开 Visual Basic 编辑器。

  4. 在 Visual Basic 编辑器中,单击“视图”菜单上的“即时窗口”。

  5. 在“即时”窗口中键入以下代码或粘贴以下代码,然后按 Enter。

    CurrentProject.Properties.Add "AllowBypassKey", False
    
  6. 关闭 Visual Basic 编辑器,然后关闭 Access 数据库项目。

  7. 打开 Access 数据库项目。 打开 Access 数据库项目时按住 SHIFT 键,尝试绕过为 Access 数据库项目设置的启动选项。

    已禁用允许绕过启动选项的 SHIFT 键的功能。 虽然按住 SHIFT 键以绕过启动选项,但会执行启动选项。 不能绕过启动选项。

Access 数据库 (.mdb 或 .accdb) 的步骤

  1. 启动 Access。

  2. 创建一个新模块,然后添加以下两个函数:

    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。