如何设置 Access 中消息框显示次数的限制

How to set a limit the number of times a message box in Access will display

我已将登录表单添加到 Access 数据库。我想找出一种方法来限制用户输入错误密码的次数。在下面显示的 VBA 代码中,当用户输入不正确的密码时,会触发以下消息框,“MsgBox "Incorrect password", vbOKOnly + vbExclamation"。

我希望用户在出现不同的消息框之前尝试失败 3 次。比如“请联系管理员”...

在此先感谢您的帮助。

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

在这种情况下,我想我会声明一个 Static 计数器变量。静态变量的值在过程调用之间保留。其他替代方法是将计数存储在全局变量中,使用 TempVar,或将它们写入 table.

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

好的,谢谢本。我进行了一些更改,现在出现错误“编译错误:没有 if 的 Else”。我想我弄错了,但很难恢复。

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