MS Access - Login - ms-access

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.

Related

Can a button check password and filter?

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

Edit user - ms access

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

MS Access 2016 - Check access level on Openform using Tempvars

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.

Microsoft Access Compile Error: Method or data member not found

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

Docmd.Close not closing the access form

I am experiencing a problem with vb in access. There is main form (say it parentForm), which has two buttons that trigger two different forms (say it childForm1 and childForm2). Both child forms perform checks on their Load events. If a condition fails, the childForm has to close. The problem is that in childForm1, the code works properly, in the childForm2 something goes completely wrong.
It seems that the close event is totally ignored. After the onLoad event, the process is carried out to the onCurrent event, which shouldn't happen! Below is the code of the onLoad event of childForm2.
Private Sub Form_Load()
On Error Resume Next
Dim db As Database
Dim rst As Recordset
Dim stDocName As String
stDocName = "childForm2"
closeEvent = False
Set db = CurrentDb
If a<> 0 And b<> 0 Then
Set rst = db.OpenRecordset("SELECT * FROM tbl1 WHERE Cust Like '*" & a & "*' AND Cust2 Like '*" & b & "*';")
If (rst.EOF Or rst.BOF) And IsLoaded(stDocName) Then
MsgBox ("No record found!")
rst.Close
SetWarnings = True
closeEvent = True
Me.SetFocus
DoCmd.Close acForm, stDocName, acSaveNo
End If
ElseIf a = 0 And b <> 0 Then
Set rst = db.OpenRecordset("SELECT * FROM tbl1 WHERE Cust2 Like '*" & b & "*';")
If (rst.EOF Or rst.BOF) Then
MsgBox ("No record found!")
rst.Close
DoCmd.Close acForm, stDocName
End If
End If
db.Close
End Sub
Also, I tried to use a global boolean variable (closeEvent in code), which is initialized to False and gets True, when the form must close. This variable is checked in the onCurrent event in order to close the form. However, when i debugged the code, the variable seems to lose its (True) value when passing from onLoad to OnCurrent event!
Any suggestion is more than appreciated.
Thanks in advance,
Maria
Use the Form_Open event instead of the Form_Loadevent.
Then, instead of closing the form (=Docmd.Close) use the built in Cancel argument to cancel the form's opening.
Private Sub Form_Open(Cancel As Integer)
If **condition not met** then
Cancel = True 'this stops the form from opening
End If
End Sub