2013-08-18 29 views
1

我在Access項目上工作,但我認爲在這個問題中沒有特定的訪問權限,所以我會將其標記爲VBA問題。這種登錄可能在VBA中嗎?

我有一個窗體,只有當你在認證用戶表(我通過他的Windows用戶名認證用戶)時纔可以打開它 - 我知道這是一個跛腳認證。

下面是我投入的形式開事件的代碼:「訪問被拒絕」

Private Sub Form_Open(Cancel As Integer) 

If DCount("User_Id", "Users", "[username]='" & (Environ$("Username")) & "'") Then 

Else 
    MsgBox "Access Denied!" 
    DoCmd.Quit 

End If 

End Sub 

我想做到的是,當MSGBOX顯示,如果我輸入某個單詞(如密碼),單擊確定按鈕,DoCmd.Quit不會執行。我不想顯示任何內容,只需輸入密碼即可。

我不需要這個絕望,我只是爲了好玩。我認爲如果VBA可能會很酷。

+1

我真的不認爲這是可能的。消息框的顯示方式並不真正允許用戶輸入,而不是按鈕。雖然我認爲它*可能*可以使用'WinAPI'來捕捉按鍵,但我確信當顯示消息框時,*無法完成,因爲代碼是*正在運行*而VBA實際上並沒有多線程以允許偵聽器在其他代碼正在執行時捕獲鍵擊。 –

回答

0

以下內容應該可以工作,但是您可能需要修改它以將密碼設置爲更好的或者您想要多個密碼。任何知道如何閱讀代碼的人都可以找到密碼,所以也許最好將它放在數據庫的某個地方?

Const sPassword As String = "PASSWORD" 
Const sMESSAGE As String = "Please enter your password" 
Const sTITLE As String = "Enter Password" 
Dim sInput As String 

sInput = InputBox(sMESSAGE, sTITLE) 

If sInput <> Password Then 
    MsgBox "Access Denied!" 
    DoCmd.Quit 
End If 
1

我在2007年進入測試這一點,我認爲邏輯是你想要的,或者至少是接近。請考慮使用類似下面的WindowsUser()函數來獲取Windows用戶名。我明白這只是爲了好玩,所以你現在不在乎。但是,爲了將來您關心的任何事情,請記住這一點。作爲安全措施的Environ("USERNAME")是輕而易舉的失敗。

Const cstrYourPassword As String = "let me in" 
Dim blnGoodbye As Boolean 
Dim lngButtons As Long 
Dim strPrompt As String 
Dim strPassword As String 

strPrompt = "Access Denied!" & vbCrLf & vbCrLf & _ 
    "Click Retry to try with password" & vbCrLf & _ 
    "or Cancel to quit." 
lngButtons = vbCritical + vbRetryCancel 
If MsgBox(strPrompt, lngButtons) = vbRetry Then 
    strPassword = InputBox("Password:") 
    If strPassword = cstrYourPassword Then 
     MsgBox "Welcome " & WindowsUser 
    Else 
     blnGoodbye = True 
    End If 
Else 
    blnGoodbye = True 
End If 

If blnGoodbye = True Then 
    MsgBox "That's all folks." 
    'DoCmd.Quit ' <- enable this when ready. 
End If 

使用此而不是Environ("USERNAME")

Public Function WindowsUser() As String 
    WindowsUser = CreateObject("WScript.Network").Username 
End Function