I have a Login system which connects to a table called Employees. I now require to make an edit user form that allows me to edit the username or password of the person logged in.
i have tried using
Me.UserName = Nz(DLookup("Username", "Employees", "Username='" & TempVars("EmployeeType2") & "'"), "")
Me.Password = Nz(DLookup("Password", "Employees", "Username='" & TempVars("EmployeeType2") & "'"), "")
But this does not allow the changes to be saved.
How would I make it so that this tempvar 9username and password) are editable in text format?
here is a the code of how my login works.
Option Compare Database
Option Explicit
Private Sub ButtonLogin_Click()
Dim rs As Recordset
Set rs = CurrentDb.OpenRecordset("Employees", dbOpenSnapshot, dbReadOnly)
rs.FindFirst "UserName='" & Me.TxtUsername & "'"
If rs.NoMatch Then
Me.LblWronguser.Visible = True
Me.TxtUsername.SetFocus
Exit Sub
End If
Me.LblWronguser.Visible = False
If rs!Password <> Nz(Me.Txtpassword, "") Then
Me.LblWrongpass.Visible = True
Me.Txtpassword.SetFocus
Exit Sub
End If
Me.LblWrongpass.Visible = False
TempVars("EmployeeType") = rs!EmployeeType_ID.Value
TempVars("EmployeeType2") = rs!UserName.Value
DoCmd.OpenForm "Home"
DoCmd.Close acForm, Me.Name
End Sub
To set the TempVars use below:
TempVars("EmployeeType") = Me.UserName.Value
TempVars("EmployeeType2") = Me.Password.Value
Related
Sorry to bother you all again...
I am trying to create a login page where upon inputting the correct UserName AND password, it would filter a subform below so that they can see only their comments. I have it so that there are two buttons where one checks the Username and Password via VBA code and another button filters using a Macro... But I would like for one button to do everything so that if the password is incorrect, then they cannot see anything. The filter button currently works without a password and as long as there is a correct username it will filter that without looking at the password.
Option Compare Database
Option Explicit
Private Sub btnLogin_Click()
Dim rs As Recordset
Set rs = CurrentDb.OpenRecordset("tbl1Employees", dbOpenSnapshot, dbReadOnly)
rs.FindFirst "UserName='" & Me.txtUserName & "'"
If rs.NoMatch Then
Me.lblWrongUser.Visible = True
Me.txtUserName.SetFocus
Exit Sub
End If
Me.lblWrongUser.Visible = False
If rs!Password <> Nz(Me.txtPassword, "") Then
Me.lblWrongPass.Visible = True
Me.txtPassword.SetFocus
Exit Sub
End If
Me.lblWrongPass.Visible = False
Dim search_text As String
search_text = Me.txtUserName
If Nz(Me.txtUserName.Value, "") = "" Then
Me.FilterOn = False
Me.txtUserName.SetFocus
Exit Sub
End If
Me.Filter = "UserName like '*" & Me.txtUserName.Value & "*' or userName like '*"
Me.FilterOn = True
Me.txtUserName.SetFocus
Me.txtUserName.Value = search_text
Me.txtUserName.SelStart = Len(Nz(Me.txtUserName. Value, "")) & 1
End Sub
I decided to just make a macro and do a docmd to run the macro at the end of the code :) thank you all
When a user has logged in i would like it so that their user name is displayed in a Label in the main menu. here is my code.this at the minute allows the login form to check details entered and compares them to the employees table to allow access to the Home form.
Option Compare Database
Option Explicit
Private Sub ButtonLogin_Click()
Dim rs As Recordset
Set rs = CurrentDb.OpenRecordset("Employees", dbOpenSnapshot, dbReadOnly)
rs.FindFirst "UserName='" & Me.TxtUsername & "'"
If rs.NoMatch Then
Me.LblWronguser.Visible = True
Me.TxtUsername.SetFocus
Exit Sub
End If
Me.LblWronguser.Visible = False
If rs!Password <> Nz(Me.Txtpassword, "") Then
Me.LblWrongpass.Visible = True
Me.Txtpassword.SetFocus
Exit Sub
End If
Me.LblWrongpass.Visible = False
TempVars("EmployeeType") = rs!EmployeeType_ID.Value
DoCmd.OpenForm "Home"
DoCmd.Close acForm, Me.Name
End Sub
after this stage i am stuck as to what the code for the "home" form to display the user that has just logged in (label) this would be the Unbound field seen.
Assuming table structure is similar to below and the label is actually textbox you can use a Dlookup function.
For this scenario, I am going to assume your textbox is called Welcome. You'll probably need to use something like this:
Set rs = CurrentDb.OpenRecordset("Employees", dbOpenSnapshot, dbReadOnly)
rs.FindFirst "UserName='" & Me.TxtUsername & "'"
If rs.NoMatch Then
Me.LblWronguser.Visible = True
Me.TxtUsername.SetFocus
Exit Sub
Else
Me.Welcome = rs!UserName
End If
Me.LblWronguser.Visible = False
If rs!Password <> Nz(Me.Txtpassword, "") Then
Me.LblWrongpass.Visible = True
Me.Txtpassword.SetFocus
Exit Sub
End If
Me.LblWrongpass.Visible = False
TempVars("EmployeeType") = rs!EmployeeType_ID.Value
DoCmd.OpenForm "Home"
DoCmd.Close acForm, Me.Name
Note, this would only apply the username to the Welcome textbox. If a name was required, you would need to reference such a column differently, such as Me.Welcome = rs!FirstName or something. Also, since there is a password verification, you probably want to restructure things so that the name would only populate after the password portion has been passed properly.
My intent is to deny users that do not meet a certain access level access to forms. I initially had issues with error code 3265 while writing the code for:
TempVars("EmployeeType").Value = rs!EmployeeType_ID.Value
This is no longer an issue; however, I cannot get access to the form even when the appropriate user is trying to enter. I've checked the spelling of table and column names multiple times as well.
Below is my code for the login (where I'm using the tempvars), followed by the code in form Load().
Option Compare Database
Option Explicit
Private Sub btnLogin_Click()
Dim rs As Recordset
Set rs = CurrentDb.OpenRecordset("Employees", dbOpenSnapshot, dbReadOnly)
rs.FindFirst "UserName='" & Me.txtUserName & "'"
If rs.NoMatch = True Then
Me.lblWrongUser.Visible = True
Me.txtUserName.SetFocus
Exit Sub
End If
Me.lblWrongUser.Visible = False
If rs!Password <> Me.txtPassword Then
Me.lblWrongPass.Visible = True
Me.txtPassword.SetFocus
Exit Sub
End If
If IsNull(Me.txtUserName) Or IsNull(Me.txtPassword) Then
MsgBox "You must enter password or login ID.", vbOKOnly + vbInformation, "Required Data"
Me.txtUserName.SetFocus
Exit Sub
End If
Me.lblWrongPass.Visible = False
If rs!EmployeeType >= 4 Then
Dim prop As Property
On Error GoTo SetProperty
Set prop = CurrentDb.CreateProperty("AllowBypassKey", dbBoolean, False)
TempVars("UserName").Value = Me.txtUserName.Value
TempVars("EmployeeType").Value = rs!EmployeeType_ID.Value
CurrentDb.Properties.Append prop
SetProperty:
If MsgBox("Would you like to turn on the bypass key?", vbYesNo, "Allow Bypass") = vbYes Then
CurrentDb.Properties("AllowBypassKey") = True
Else
CurrentDb.Properties("AllowBypassKey") = False
End If
End If
Me.Visible = False
DoCmd.OpenForm "frmMain"
Globals.LoggingSignOn "Logon"
End Sub
Private Sub Form_Load()
Me.txtUserName = Null
Me.txtPassword = Null
Me.txtUserName.SetFocus
End Sub
Private Sub Form_Unload(Cancel As Integer)
Globals.LoggingSignOn "Logoff"
End Sub
Private Sub Form_Load()
If Nz(DLookup("HasAccess", "tbl9EmployeeAccess", "EmployeeType_ID=" & TempVars("EmployeeType") & " FormName='" & Me.Name & "'"), False) = False Then
MsgBox "You do not have access to access this location."
DoCmd.Close acForm, Me.Name
End If
End Sub
Thank you for your time, to anybody that looks into this.
I am creating a simple login form for my Database. When I click login, the message "Compile Error: Method or data member not found" appears. How do I fix that? Thanks! Code is below
Option Compare Database
Option Explicit
Private Sub btnLogin_Click()
Dim rs As Recordset
Set rs = CurrentDb.OpenRecordset("TBL:Staff", dbOpenSnapshot, dbReadOnly)
rs.FindFirst "UserName='" & Me.txtUserName & "'"
If rs.NoMatch = True Then
Me.lblWrongUser.Visible = True
Me.txtUserName.SetFocus
Exit Sub
Me.lblWrongUser.Visible = False
If rs!Password <> Nz(Me.txtPassword, "") Then
Me.lblWrongPass.Visible = True
Me.txtPassword.SetFocus
Exit Sub
End If
Me.lblWrongPass.Visible = False
DoCmd.OpenForm "FRM:Customer"
DoCmd.Close acForm, Me.Name
End Sub
Try this.
Check username and password values have been provided, then see if they exist in the database by a simple DCount.
If the username/password exist it will return > 0 and if not, it will return 0.
Private Sub btnLogin_Click()
With Me
'Username/Password - value provided?
If IsNull(.txtUserName.Value) Or IsNull(.txtPassword.Value) Then
MsgBox "Both fields required.", vbExclamation
Exit Sub
End If
'Username exists in Table?
If DCount("*", "Staff", "UserName='" & .txtUserName.Value & "'") = 0 Then
.lblWrongUser.Visible = True
.txtUserName.SetFocus
Exit Sub
End If
'Password exists in Table?
If DCount("*", "Staff", "UserName='" & .txtUserName.Value & _
"' And Password='" & .txtPassword.Value & "'") = 0 Then
.lblWrongPass.Visible = True
.txtPassword.SetFocus
Exit Sub
End If
End With
'Code will reach here only if supplied username and passowrd are correct
With DoCmd
.OpenForm "Customer", acNormal, , , acFormPropertySettings, acWindowNormal
.Close acForm, Me.Name, acSavePrompt
End With
End Sub
I'm using DLookup to search for a field in a table. It runs correctly, but is slow. Is there anything I can do to speed it up?
Here's my existing code:
Private Sub cmdLogin_Click()
strUserLevel = ""
If IsNull(Me.cmbUserName) Or Me.cmbUserName = "" Then
MsgBox "You must enter a User Name.", vbOKOnly, "Required Data"
Me.cmbUserName.SetFocus
Exit Sub
End If
If IsNull(Me.txtPassword) Or Me.txtPassword = "" Then
MsgBox "You must enter a Password.", vbOKOnly, "Required Data"
Me.txtPassword.SetFocus
Exit Sub
End If
'strUserName = cmbUserName.Value
If Me.txtPassword.Value = DLookup("Password", "tableUser", "[lngEmpID]=" & Me.cmbUserName.Value) Then
lngMyEmpID = Me.cmbUserName.Value
strUserLevel = DLookup("Department", "tableUser", "[lngEmpID]=" & Me.cmbUserName.Value)
strUserName = DLookup("User_Name", "tableUser", "[lngEmpID]=" & Me.cmbUserName.Value)
boolInventoryMDL = DLookup("Inventory", "tableDepartment", "[Department]=""" & strUserLevel & """")
boolDispositionMDL = DLookup("Disposition", "tableDepartment", "[Department]=""" & strUserLevel & """")
boolReviewCloseMDL = DLookup("Review", "tableDepartment", "[Department]=""" & strUserLevel & """")
boolAdministratorMDL = DLookup("Administrator", "tableDepartment", "[Department]=""" & strUserLevel & """")
boolUserListMDL = DLookup("UserList", "tableDepartment", "[Department]=""" & strUserLevel & """")
boolUserLevelMDL = DLookup("UserLevel", "tableDepartment", "[Department]=""" & strUserLevel & """")
If strUserLevel = "Superuser" Then
MsgBox "Welcome back Superuser! You can access all the modules here..", vbOKOnly, "Caution"
Else
MsgBox "Welcome! Login Success!", vbOKOnly, "Login Page"
End If
DoCmd.Close acForm, "frmLogin", acSaveNo
DoCmd.OpenForm "frmModule"
Else
MsgBox "Password Invalid. Please Try Again", vbOKOnly, "Invalid Entry!"
Me.txtPassword.Value = ""
Me.txtPassword.SetFocus
End If
End Sub
I don't believe the problem is due to inherent slowness of DLookup. Rather the problem is that the code uses so many of them.
Open one recordset based on a query of tableUser and take the values you need from that recordset. Then open a second recordset from a query of tableDepartment and get your remaining values.
Dim db As DAO.database
Dim qdf As DAO.QueryDef
Dim rs As DAO.Recordset
Dim strSelect As String
strSelect = "SELECT u.Password, u.Department, u.User_Name" & vbCrLf & _
"FROM tableUser AS u WHERE u.lngEmpID = [which_EmpId];"
Set db = CurrentDb
Set qdf = db.CreateQueryDef(vbNullString, strSelect)
qdf.Parameters("which_EmpId") = Me.cmbUserName
Set rs = qdf.OpenRecordset(dbOpenSnapshot)
If Not rs.EOF Then
If rs![Password] = Me.txtPassword Then
strUserLevel = rs!Department
strUserName = rs!User_Name
rs.Close
' open another recordset from a query of tableDepartment
' to retrieve your bool????? values
End If
End If
In that abbreviated sample, I used a temporary QueryDef for the parameterized SELECT query. However you would be better of to save that SQL as a named query, perhaps qryFetchUserData. Then at run time, instead of recreating the query each time, you could simply open the saved query.
Set qdf = db.QueryDefs("qryFetchUserData")
For optimum performance, you should add indexes on tableUser.lngEmpID and tableDepartment.Department if they're not already indexed.