Access 2007 開啟時只顯示表單

Posted by Eric... On 2016年7月1日 星期五 0 意見

客戶需求:
一般使用者進入 Access 2007 只顯示表單(Form),將資料表隱藏起來,並且無法直接存取資料表(Table)。避免一般使用者直接更改資料表內容。

經過 Google 查詢國內外的資料,確認只要加上以下的 code 於VBA程式碼,再重新啟動就看不到表單之外的資料。那開發人員或是管理者呢?處理方式為開啟Access檔案時,按下 Shift 鍵就會跳過以下程序。

Sub SetStartupProperties()
Const DB_Text As Long = 10
Const DB_Boolean As Long = 1
    ChangeProperty "StartupForm", DB_Text, "首頁"  ‘修改為啟動表單頁
    ChangeProperty "StartupShowDBWindow", DB_Boolean, False
    ChangeProperty "StartupShowStatusBar", DB_Boolean, False
    ChangeProperty "StartupMenuBar", DB_Boolean, False
    ChangeProperty "AllowShortcutMenus", DB_Boolean, False
    ChangeProperty "AllowBuiltInToolbars", DB_Boolean, False
    ChangeProperty "AllowFullMenus", DB_Boolean, False
    ChangeProperty "AllowBreakIntoCode", DB_Boolean, False
    ChangeProperty "AllowSpecialKeys", DB_Boolean, True
    ChangeProperty "AllowBypassKey", DB_Boolean, True
   
    DoCmd.ShowToolbar "Ribbon", acToolbarNo
    DoCmd.ShowToolbar "Status Bar", acToolbarNo
    DoCmd.NavigateTo "acNavigationCategoryObjectType"
    DoCmd.RunCommand acCmdWindowHide
   
End Sub

Function ChangeProperty(strPropName As String, varPropType As Variant, varPropValue As Variant) As Integer
    Dim dbs As Object, prp As Variant
    Const conPropNotFoundError = 3270

    Set dbs = CurrentDb
    On Error GoTo Change_Err
    dbs.Properties(strPropName) = varPropValue
    ChangeProperty = True

Change_Bye:
    Exit Function

Change_Err:
    If Err = conPropNotFoundError Then ' 找不到屬性。
     Set prp = dbs.CreateProperty(strPropName, _
     varPropType, varPropValue)
     dbs.Properties.Append prp
     Resume Next
    Else
     ' 未知的錯誤。
     ChangeProperty = False
     Resume Change_Bye
    End If
End Function

READ MORE