如何设置限制访问中的消息框的次数将显示

发布于 2025-01-24 02:37:10 字数 909 浏览 2 评论 0原文

我已经在访问数据库中添加了登录表单。我正在尝试找出一种限制用户输入不正确密码的次数的方法。在下面显示的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

I have added a login form to an Access Database. I am trying to figure out a way to limit the number of times a user can enter an incorrect password. In the VBA code shown below, when the user inputs a password that is incorrect, the following message box triggers, "MsgBox "Incorrect password", vbOKOnly + vbExclamation".

I would like the user to get three failed attempts before a different message box appears. Something like, "Please contact administrator"...

Thanks in advance for the help.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小苏打饼 2025-01-31 02:37:10

在这种情况下,我认为我会声明static计数器变量。静态变量的值在过程调用之间保留。其他替代方法是将计数存储在AA全局变量中,使用TeenPVAR或将其写入表。

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

In this situation I think I'd declare a Static counter variable. The value of a static variable is preserved between procedure calls. Other alternatives would be to store the count in a a global variable, use a TempVar, or write them to a 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
勿忘初心 2025-01-31 02:37:10

好的,谢谢本。我进行了一些更改,现在遇到了错误,“编译错误:没有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

Okay thank you Ben . I have made some changes and am now getting error, "Compile error: Else without if". I think I got things out of order, but am having a tough time getting it back.

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文