Microsoft Access 有兩個的內建的安全性功能,來保護您的資料庫:
本文將告訴您您也可以在您的資料庫中設定個別的密碼,每個表單,以及每個報表的方式。
Microsoft 僅,為了說明提供程式設計範例,不提供任何明示或默示的保證。這包括,但不限於適售性或適合某特定用途之默示擔保責任。本文假設您已熟悉使用我們所示範的程式設計語言以及建立和偵錯程序所使用的工具。Microsoft 技術支援工程師可以協助解釋特定程序的功能,但它們不會修改這些範例以提供附加功能或建構程序,以符合您特定需求。
警告: 如果您依照本範例,修改範例資料庫 Northwind.mdb。您可能要備份 Northwind.mdb 檔案和資料庫的副本上依照下列步驟執行。
注意: 本文中的範例程式碼會使用 Microsoft 資料存取物件。這個程式碼正常執行,您必須參考 Microsoft DAO 3.6 物件程式庫。以執行這項操作,按一下
引用項目 在 [
工具] 功能表中 Visual Basic 編輯器] 中,請確定已選取
Microsoft DAO 3.6 物件程式庫] 核取方塊。
使用程式碼,以密碼保護表單
利用程式碼您可以在使用者開啟表單或報表時提示輸入密碼。如果輸入正確的密碼被開啟表單或報表。
下列範例會示範您如何可以密碼保護在範例資料庫 Northwind.mdb 中的 [訂單] 表單:
- 啟動 Access,然後開啟 [範例資料庫 Northwind.mdb。
- 按下 ALT + F11 即可啟動 Microsoft Visual Basic 編輯器。
- 在 插入] 功能表上按一下 [模組]。
- 在的模組工作表中輸入下列的程序:
Public MyPassword
Public Function KeyCode(Password As String) As Long
' This function will produce a unique key for the
' string that is passed in as the Password.
Dim I As Integer
Dim Hold As Long
For I = 1 To Len(Password)
Select Case (Asc(Left(Password, 1)) * I) Mod 4
Case Is = 0
Hold = Hold + (Asc(Mid(Password, I, 1)) * I)
Case Is = 1
Hold = Hold - (Asc(Mid(Password, I, 1)) * I)
Case Is = 2
Hold = Hold + (Asc(Mid(Password, I, 1)) * _
(I - Asc(Mid(Password, I, 1))))
Case Is = 3
Hold = Hold - (Asc(Mid(Password, I, 1)) * _
(I + Len(Password)))
End Select
Next I
KeyCode = Hold
End Function
- 按下 ALT + F11,返回 Access。
- 在 物件,] 下的 [資料庫] 視窗中按一下 [表格,然後按一下 [新增]。
- 在 新增資料表] 對話方塊中,按兩下 [設計檢視]。
- 建立新的資料表,如下所示:
Table: tblPassword
---------------------------
Field Name: ObjectName
Data Type: Text
Field Size: 50
Field Name: KeyCode
Data Type: Text
Field Size: 25
Input Mask: Password
Table Properties: tblPassword
-----------------------------
PrimaryKey: ObjectName
- 開啟 tblPassword 資料表,然後再輸入下列資料:
ObjectName: Orders
KeyCode: 2818
- 在設計檢視中建立新表單,並將表單儲存為 frmPassword。
- 新增單一文字方塊稱為 Text0,frmPassword 和呼叫 CheckPassword 指令按鈕。
- 設定 Text0 密碼 」 的 [輸入遮罩 屬性 (不含引號)。
- 將下列程式碼加入至 CheckPassword 按鈕的 OnClick 事件,然後儲存表單:
If IsNull(Forms!frmPassword!Text0.Value) Then
MsgBox "You cannot enter a blank Password. Try again."
Me!Text0.SetFocus
Else
MyPassword = Me!Text0.Value
DoCmd.Close acForm, "frmPassword"
End If
- 在 [設計] 檢視中開啟 [訂單] 表單。
- 如果看不到屬性工作表,請按一下 [檢視] 功能表上的 [內容]。
- 在模組的表單的 OnOpen 屬性中輸入下列的事件程序:
Private Sub Form_Open(Cancel as Integer)
Dim Hold As Variant
Dim tmpKey As Long
Dim I As Integer
Dim rs As DAO.Recordset
Dim db As DAO.Database
On Error GoTo Error_Handler
' Prompt the user for the Password.
DoCmd.OpenForm "frmPassword", acNormal, , , , acDialog
Hold = MyPassword
' Open the table that contains the password.
Set db = CurrentDb
Set rs = db.OpenRecordset("tblPassword", dbOpenTable)
rs.Index = "PrimaryKey"
rs.Seek "=", Me.Name
If rs.NoMatch Then
MsgBox "Sorry cannot find password information. Try Again"
Cancel = -1
Else
' Test to see if the key generated matches the key in
' the table; if there is not a match, stop the form
' from opening.
If Not (rs![keycode] = KeyCode(Cstr(Hold))) Then
MsgBox "Sorry you entered the wrong password." & _
"Try again.", vbOKOnly, "Incorrect Password"
Cancel = -1
End If
End If
rs.Close
db.Close
Exit Sub
Error_Handler:
MsgBox Err.Description, vbOKOnly, "Error #" & Err.Number
Exit Sub
End Sub
- 關閉並再儲存 [訂單] 表單。
- 開啟 [訂單] 表單,然後鍵入 密碼,當系統提示的密碼。
請注意 [訂單] 表單會開啟。KeyCode 所產生的 密碼 符合 KeyCode tblPassword] 表格中的,而是取決於輸入密碼中字母大小寫。 - 關閉並再重新開啟 [訂單] 表單,然後鍵入 密碼,當系統提示密碼的]。
請注意您會收到訊息: 因為密碼程序是區分大小寫,則不會開啟 [訂單] 表單。 - 要判斷相對應的 KeyCode 為一特定字串,在 [即時運算] 視窗中輸入下列命令並按下 ENTER: 稍早的範例會傳回 5864。
- 若要隱藏 tblPassword 資料表,在 [資料庫] 視窗中的,tblPassword] 表格上按一下滑鼠右鍵,然後再按 [內容]。在 [屬性] 視窗中按一下以選取 [隱藏] 核取方塊,然後再按一下 [確定]。