Alex,
Of course it's going to return 0, because you haven't set the variable to
anything prior to reading it! But I fail to see why you need this variable
anyway. In fact, if all you want to do is allow the user 3 attempts at
logging in, then the following will do it:
'Declarations Section
Private intLogonAttempts As Integer
Private Sub Command5_Click()
Dim rs As DAO.Recordset
Dim sSQL As String
If Len(Trim(Nz(Me.user_name, ""))) = 0 _
Or Len(Trim(Nz(Me.user_password, ""))) = 0 Then
MsgBox "You must enter a Username and Password.", vbOKOnly,
"Required Data"
Me.user_name = ""
Me.user_password = ""
Me.user_name.SetFocus
Else
sSQL = "SELECT user_name, user_password " & _
"FROM [User] " & _
"WHERE user_id = """ & Me.user_name & _
""" AND user_password = """ & Me.user_password & """"
Set rs = DBEngine(0)(0).OpenRecordset(sSQL, dbOpenSnapshot)
If rs.AbsolutePosition > -1 Then
'Allow the user to login
Else
intLoginAttempts = intLoginAttempts + 1
End If
End If
rs.Close
Set rs = Nothing
If (intLogonAttempts = 3) Then
MsgBox "You do not have access to this database." & vbCrLf & vbCrLf
& _
"Please contact your system administrator.",
vbCritical, "Restricted Access!"
Application.Quit
End If
End Sub
Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia
Microsoft Access 2003 VBA Programmer's Reference
http://www.wiley.com/WileyCDA/WileyTitle/productCd-0764559036.html
Alex via AccessMonster.com said:
Hi Graham, thanks for your reply
I followed your instructions, seems like a good idea, but Im getting "0"
in
the text box field, when it is supposed to be either 1,2,3 (depends on
what
I select from combo box). Its a log in form with cb with the code:
---------
Private Sub Command5_Click()
'Check to see if data is entered into the UserName combo box
If IsNull(Me.user_name) Or Me.user_name = "" Then
MsgBox "You must enter a User Name.", vbOKOnly, "Required Data"
Me.user_name.SetFocus
Exit Sub
End If
'Check to see if data is entered into the password box
If IsNull(Me.user_password) Or Me.user_password = "" Then
MsgBox "You must enter a Password.", vbOKOnly, "Required Data"
Me.user_password.SetFocus
Exit Sub
End If
'Check value of password in tblEmployees to see if this matches value
chosen in combo box
If Me.user_password.Value = DLookup("user_password", "User", "[user_id]
=" & Me.user_name.Value) Then
Me.user_name.Value = myVariable
'Close logon form and open splash screen
DoCmd.Close acForm, "User", acSaveNo
DoCmd.OpenForm "MainMenu"
Else
MsgBox "Password Invalid. Please Try Again", vbOKOnly, "Invalid
Entry!"
Me.user_password.SetFocus
End If
'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
your system administrator.", vbCritical, "Restricted Access!"
Application.Quit
End If
End Sub
----------------------
As you can see, I've got Me.user_name.Value = myVariable
user_name is the name of the combo box.
What must I look at to resolve "0" problem? I have no idea myself
God bless me and all those who help people on this forum, this is the last
problem I need to solve before the DB completely finished