I think this might work but the following line is giving me fits:
'Get stored password
sStoredPass = Nz(DLookup("Password", "Staff Extended", "Password = '" &
sUser & "'"), "")
This is alwas coming blank.
Password is the name of the field
Staff Extended is the name of the query
sUser has the right USerID so I am not sure what is going on.
--
Thanks,
Art
Database Administrator
Yankton, SD
:
In a new or existing standard module:
'CODE START
Public Function udf_Login(sUser As String, _
sPass As String) As Long
On Error Goto Error_udf_Login
Dim Ret as Long 'Return Value
Dim sStoredPass as String 'Correct Password from user table
Static iLogAttempts as Integer 'Number of Login Attempts
Ret = 0 'Initialize Return
iLogAttempts = iLogAttempts + 1
'Verify acceptable login attempts
If iLogAttempts > 3 Then
MsgBox "You do not have access to this database..."
Application.Quit
End If
'Verify User
If Len(sUser) = 0 Then
Ret = 2
MsgBox "Please select a User to login."
Goto Exit_udf_Login
End If
'Get Stored Password
sStoredPass = Nz(Dlookup("MyPassField", "MyPassTable", "MyPassField = '" _
& sUser & "'"), "")
'Verify Password
If (Instr(1, sStoredPass, sPass) <> 1) Or (Len(sStoredPass) <> Len(sPass))
Then
Ret = 3
MsgBox "Password Incorrect, Please try again."
Goto Exit_udf_Login
End If
'User & Pass verified
DoCmd.Close acForm, "Login Dialog", acSaveNo
Docmd.OpenForm "Home"
'Save the logged user to somwhere, in this case to a control on form Home
Forms!Home.Controls!ctlCurrUser.Value = sUser
'If everything went well and the function made it this far...
iLogAttempts = 0 'Reset Login Attempt Counter
Ret = 1 'Return a value (not required in this case but good practice
nonetheless)
Exit_udf_Login:
udf_login = Ret
Exit Function
Error_udf_Login:
Ret = 0
Resume Exit_udf_Login
Resume
End Function
'CODE END
And in the btnLogin_Click event of form Login Dialog:
'CODE START
Private Sub btnLogin_Click
Me.txtPassword.SetFocus
udf_Login Nz(Me.cboEmployee, "") Nz(Me.txtPassword.Text, "")
End Sub
This code is UNTESTED and may be subject to fine tuning. I would strongly
suggest renaming your forms to "frmLoginDialog" and "frmHome" Or at least
anything else besides Home... that sounds very much like a reserved word in
some reference or another.
-jack
:
Could you help me write this as a function? Is it as simple as changing my
reference to my controls (Me.cboEmployee and Me.txtPassword) to reference
sUSer and sPass?
Public Function udf_Login(sUser, sPass)
'Check to see if data is entered into the UserName combo box
If IsNull(Me.cboEmployee) Or Me.cboEmployee = "" Then
MsgBox "You must enter a User Name.", vbOKOnly, "Required Data"
Me.cboEmployee.SetFocus
Exit Function
End If
'Check to see if data is entered into the password box
If IsNull(Me.txtPassword) Or Me.txtPassword = "" Then
MsgBox "You must enter a Password.", vbOKOnly, "Required Data"
Me.txtPassword.SetFocus
Exit Function
End If
'Check value of password in tblEmployees to see if this matches value chosen
in combo box
If Me.txtPassword.Value = DLookup("Password", "tblStaff", "[ID]=" &
Me.cboEmployee.Value) Then
lngMyEmpID = Me.cboEmployee.Value
'Close logon form and open splash screen
DoCmd.Close acForm, "Login Dialog", acSaveNo
DoCmd.OpenForm "Home"
Else
MsgBox "Password Invalid. Please Try Again", vbOKOnly, "Invalid
Entry!"
Me.txtPassword.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 Function
--
Thanks,
Art
Database Administrator
Yankton, SD
:
I not sure about doing that in a macro, I don't generally use them. If I had
to run a login procedure off a macro I would just make a function to call
from the macro that does the checks and opens the form if everything passes.
in the macro:
RunCode
=fLogin(Me.ctlUser, Me.ctlPass)
and the function:
Public Function fLogin(sUser, sPass)
'check that the user exists
'make sure user isnt already logged on
'verify password
'open frmHome and close frmLogin
End Function
As far as the TempVars I'm not really sure, I don't have any experience with
them (they're new to 07 i think? i'm on 03), but I run an Active Users table.
A record of the user, computer and the winuser are logged from fLogin and
fLogout on my frmHome_Close event removes the entry (and other cleanup..).
This might be a little off leauge with what you were looking, I wouldn't
know how to handle it with a macro, but I just got done wrapping up a solid
login/out procedure about a month ago and that worked out well for me.
:
Not sure how to approach that. Woould there be a way to do the password
validation within the Macro?
--
Thanks,
Art
Database Administrator
Yankton, SD
:
Taken from:
http://office.microsoft.com/en-us/access/HA101202161033.aspx
Temporary variables are global. Once a temporary variable has been created,
you can refer to it in an event procedure (event procedure: A procedure that
is automatically executed in response to an event initiated by the user or
program code, or that is triggered by the system.), a Visual Basic for
Applications (VBA) (Visual Basic for Applications (VBA): A macro-language
version of Microsoft Visual Basic that is used to program Microsoft
Windows-based applications and is included with several Microsoft programs.)
module, a query, or an expression. For example, if you created a temporary
variable named MyVar, you could use the variable as the control source for a
text box by using the following syntax:
=[TempVars]![MyVar]
On a side note, in case you weren't aware, your password verification is not
case sensitive
Me.txtPassword.Value = DLookup("Password", "tblStaff", "[ID]=" &
Me.cboEmployee.Value)
Will return true regardless of case. Maybe you're fine with this, but if
you do want case sensitive, I use the following:
If (Instr(1, <StoredPass>, <UserInputPass>, vbBinaryCompare) = 1) And _
(Len(<StoredPass>) = Len(<UserInputPass>)) Then
'Password Verification OK
End If
HTH
-jack
:
I have a bit of a quandry. I have a maco that uses SetTempVar to set the
CurrentUserID so I can filter on records for that user. This is the macro:
Condition Action Arguments
Not IsNull([cboCurrentStaff]) SetTempVar CurrentUserID, [cboCurrentStaff]
Close ,,Prompt
OpenForm Home, Form, , , , Normal
StopMacro
MsgBox You must first select a staff member., Yes, None,
The specification now calls for checking for passwords and other
validations. I created the following procedure:
Private Sub cmdLogin_Click()
'Check to see if data is entered into the UserName combo box
If IsNull(Me.cboEmployee) Or Me.cboEmployee = "" Then
MsgBox "You must enter a User Name.", vbOKOnly, "Required Data"
Me.cboEmployee.SetFocus
Exit Sub
End If
'Check to see if data is entered into the password box
If IsNull(Me.txtPassword) Or Me.txtPassword = "" Then
MsgBox "You must enter a Password.", vbOKOnly, "Required Data"
Me.txtPassword.SetFocus
Exit Sub
End If
'Check value of password in tblEmployees to see if this matches value chosen
in combo box
If Me.txtPassword.Value = DLookup("Password", "tblStaff", "[ID]=" &
Me.cboEmployee.Value) Then
lngMyEmpID = Me.cboEmployee.Value
'Close logon form and open splash screen
DoCmd.Close acForm, "Login DialogNew", acSaveNo
DoCmd.OpenForm "Home"