Thanks the following code works, but is this the best way it could be
done.....this subroutine is not in a module but behind a command button
within the form. And if two people are using the database @ the same
time...do this static declared variable works independently within a user
sessions?
Private Sub Login_Click()
'Check to see if data is entered into the UserName combo box
If IsNull(Me.UName) Or Me.UName = "" Then
MsgBox "You must select a username.", vbOKOnly, "Required Data"
Me.UName.SetFocus
Exit Sub
End If
'Check to see if data is entered into the password box
If IsNull(Me.PWord) Or Me.PWord = "" Then
MsgBox "You must enter a Password.", vbOKOnly, "Required Data"
Me.PWord.SetFocus
Exit Sub
End If
' Open the table that contains the password.
'Declare and instantiate a Recordset Object
Dim rst As ADODB.Recordset
Set rst = New ADODB.Recordset
'Establish the connection and cursor type, and open the recordset
rst.ActiveConnection = CurrentProject.Connection
rst.CursorType = adOpenStatic
rst.Open "Select * FROM Password2 WHERE UserID = " & Me.UName.Value
'Check value of password to see if this matches value chosen in combo box
'Test Code: If Me.PWord.Value = DLookup("Password", "Password2", "[UserID]="
& Me.UName.Value) Then
If Me.PWord.Value = rst("Password") Then
MyUserID = Me.UName.Value
'Close logon form and open main switchboard
DoCmd.Close acForm, "Login2", acSaveNo
DoCmd.OpenForm "Main Switchboard"
Else
MsgBox "Password Invalid. Please Try Again", vbOKOnly, "Invalid Entry!"
Me.PWord.SetFocus
End If
rst.Close
Set rst = Nothing
'If User Enters incorrect password 3 times database will shutdown
**Static intLogonAttempts As Integer**
intLogonAttempts = intLogonAttempts + 1
If intLogonAttempts = 3 Then
MsgBox "You do not have access to this database.Please contact admin.",
vbCritical, "Restricted Access!"
Application.Quit
End If
End Sub
Douglas J Steele said:
I don't see
Dim intLogonAttempts As Integer
anywhere.
That means it's local to Login_Click, and will get reset to 0 each time
the
routine's called.
You should use Option Explicit at the beginning of the code to ensure that
you've declared all variables. (To automatically get Option Explicit in
your
modules, select the Module tab under Tools | Options, and check the
"Require
Variable Declaration" box)
intLogonAttempts either needs to be declared outside of the module, or
else
as Static inside the module:
Static intLogonAttempts As Integer
--
Doug Steele, Microsoft Access MVP
(no e-mails, please!)
all code
Option Compare Database
Private Sub UName_AfterUpdate()
'After selecting username set focus to password field
Me.PWord.SetFocus
End Sub
Private Sub Login_Click()
'Check to see if data is entered into the UserName combo box
If IsNull(Me.UName) Or Me.UName = "" Then
MsgBox "You must select a username.", vbOKOnly, "Required Data"
Me.UName.SetFocus
Exit Sub
End If
'Check to see if data is entered into the password box
If IsNull(Me.PWord) Or Me.PWord = "" Then
MsgBox "You must enter a Password.", vbOKOnly, "Required Data"
Me.PWord.SetFocus
Exit Sub
End If
' Open the table that contains the password.
'Declare and instantiate a Recordset Object
Dim rst As ADODB.Recordset
Set rst = New ADODB.Recordset
'Establish the connection and cursor type, and open the recordset
rst.ActiveConnection = CurrentProject.Connection
rst.CursorType = adOpenStatic
rst.Open "Select * FROM Password2 WHERE UserID = " & Me.UName.Value
'Check value of password to see if this matches value chosen in combo box
'Test Code: If Me.PWord.Value = DLookup("Password", "Password2",
"[UserID]="
& Me.UName.Value) Then
If Me.PWord.Value = rst("Password") Then
MyUserID = Me.UName.Value
'Close logon form and open main switchboard
DoCmd.Close acForm, "Login2", acSaveNo
DoCmd.OpenForm "Main Switchboard"
Else
MsgBox "Password Invalid. Please Try Again", vbOKOnly, "Invalid
Entry!"
Me.PWord.SetFocus
End If
rst.Close
Set rst = Nothing
'If User Enters incorrect password 3 times database will shutdown
intLogonAttempts = intLogonAttempts + 1
If intLogonAttempts > 3 Then
MsgBox "You do not have access to this database.Please contact
admin.",
vbCritical, "Restricted Access!"
Application.Quit
End If
End Sub
message
You haven't shown enough code then.
Where is intLogonAttempts defined? Is there any other code that refers
to
intLogonAttempts?
--
Doug Steele, Microsoft Access MVP
(no e-mails, please!)
i'm sorry it intLogonAttempts = intLogonAttempts + 1 and still noy
working
any ideas...thanks.
message
If that's an actual copy-and-paste, it would appear that you have a
typo.
intLogonAttempts = intLogonAttempts = 1
should be
intLogonAttempts = intLogonAttempts + 1
Of course, I wouldn't expect intLogonAttempts ever to be reset to 1.
I
would
expect it to alternative between 0 (False) and -1 (True)
--
Doug Steele, Microsoft Access MVP
(no e-mails, please!)
why intLogonAttempts never become greater than 3... all other code
works
great, but intLogonAttempts sets itself back to 1 after every
button
click...
Private Sub Login_Click()
'Check to see if data is entered into the UserName combo box
'Check to see if data is entered into the password box
' Open the table that contains the password.
'Declare and instantiate a Recordset Object
'Establish the connection and cursor type, and open the
recordset
'Check value of password to see if this matches value chosen in
combo
box
'Close logon form and open main switchboard
'If User Enters incorrect password 3 times database will
shutdown
intLogonAttempts = intLogonAttempts = 1
If intLogonAttempts > 3 Then
MsgBox "You do not have access to this database.Please contact
admin.",
vbCritical, "Restricted Access!"
Application.Quit
End If
End Sub