我已將登錄表單添加到 Access 資料庫。我試圖找出一種方法來限制用戶輸入錯誤密碼的次數。在如下所示的 VBA 代碼中,當用戶輸入的密碼不正確時,會觸發以下訊息框,“MsgBox “密碼錯誤”,vbOKOnly vbExclamation。
我希望用戶在出現不同的訊息框之前獲得三次失敗的嘗試。諸如“請聯系管理員”之類的...
在此先感謝您的幫助。
Private Sub OkBTN_Click()
'Check that User is selected
If IsNull(Me.cboUser) Then
MsgBox "You forgot to select your name from the drop down menu!", vbCritical
Me.cboUser.SetFocus
Else
'Check for correct password
If Me.txtPassword = Me.cboUser.Column(2) Then
'Check if password needs to be reset
If Me.cboUser.Column(4) Then
DoCmd.OpenForm "frmPasswordChange", , , "[UserID] = " & Me.cboUser
End If
Me.Visible = False
Else
MsgBox "Incorrect password", vbOKOnly vbExclamation
Me.txtPassword = Null
Me.txtPassword.SetFocus
End If
End If
End Sub
uj5u.com熱心網友回復:
在這種情況下,我想我會宣告一個Static計數器變數。靜態變數的值在程序呼叫之間保留。其他替代方法是將計數存盤在全域變數中、使用 TempVar 或將它們寫入表中。
Private Sub OkBTN_Click()
Static intIncorrectCount As Integer ' The value of a static variable is preserved between procedure calls.
'Check that User is selected
If IsNull(Me.cboUser) Then
MsgBox "You forgot to select your name from the drop down menu!", vbCritical
Me.cboUser.SetFocus
Else
'Check for correct password
If Me.txtPassword = Me.cboUser.Column(2) Then
'Check if password needs to be reset
If Me.cboUser.Column(4) Then
DoCmd.OpenForm "frmPasswordChange", , , "[UserID] = " & Me.cboUser
End If
Me.Visible = False
intIncorrectCount = 0
ElseIf intIncorrectCount > 2 Then
MsgBox "Too many incorrect attempts. Closing Access.", vbOKOnly vbExclamation
'DoCmd.Quit ' ### Save your code before enabling this line. ###
Else
MsgBox "Incorrect password", vbOKOnly vbExclamation
Me.txtPassword = Null
Me.txtPassword.SetFocus
intIncorrectCount = intIncorrectCount 1
End If
End If
End Sub
uj5u.com熱心網友回復:
好的,謝謝本。我做了一些更改,現在出現錯誤,“編譯錯誤:否則沒有 if”。我想我把事情搞砸了,但我很難把它找回來。
Private Sub OkBTN_Click()
Static intIncorrectCount As Integer
Dim AuthorityNumber As Integer
'Dim rs As Recordset
'Column definitions
'UserID = 0
'FullName = 1
'Password = 2
'PWReset = 3
'AccessLevelID = 4
'Set rs = CurrentDb.OpenRecordset("UserNameQuery", dbOpenSnapshot)
'N = Nz(DLookup("Fullname", "UserNameQuery", "Fullname=""" & Me.cboUser & """"), " ")
If IsNull(Me.cboUser) Then 'Check that User is selected
MsgBox "You forgot to select your name from the drop down menu!", vbCritical
Me.cboUser.SetFocus
Else
If Me.txtPassword = Me.cboUser.Column(2) Then 'Check for correct password
If Me.cboUser.Column(3) Then 'Check if password needs to be reset
DoCmd.OpenForm "frmPasswordChange", , , "[UserID] = " & Me.cboUser
End If
If Me.cboUser.Column(4) Then
DoCmd.OpenForm "SRL1MainMenu"
Forms!SRL1MainMenu!FullNameLoggedIn = Forms!frmLogin!cboUser.Column(1)
Else
DoCmd.OpenForm "L2MainMenu2"
Forms!L2MainMenu2!FullNameLoggedIn = Forms!frmLogin!cboUser.Column(1)
ElseIf intIncorrectCount > 1 Then
MsgBox "Too many failed login attempts. Click OK to set new password", vbOK vbExclamation
DoCmd.OpenForm "frmPasswordChange", , , "[UserID] = " & Me.cboUser
'DoCmd.Close acForm, "frmLogin"
Else
MsgBox "Incorrect password", vbOKOnly vbExclamation
Me.Visible = False
intIncorrectCount = 0
Me.txtPassword = Null
Me.txtPassword.SetFocus
intIncorrectCount = intIncorrectCount 1
End If
End If
TempVars("Username") = Me.cboUser.Value
End If
End If
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/468356.html
上一篇:反透視訪問資料表
