PDA

View Full Version : VBA Code for Login for multiple usernames and forms



emmorel
05-10-2016, 08:07 AM
I want to know how I can modify my VBA code for a database Login function that can be used by multiple users and when each user logs in it will open a custom form.

Right now I have a tblEmployees that contains Username (email address), SecurityID and Password. I have a separate tblUser with Userlevel, UserSecurity, Userlogin (email address), Dbform name, Password

Below is the code I started with for a database Login that requires a username and password and based on the username level a specific form opens up; right now is either one form or the other.
I've been asked to produce a function that will open different forms depending on the user that logs in and I'm stuck with this one and don't even know if it can be done. Any assistance you can provide is appreciated.

emmorel
Private Sub btnLogIn_Click()
Dim UserLevel As Integer
TempVars("Username").Value = Me.txtUsername.Value
If IsNull(Me.txtUsername) Then
MsgBox "Please enter Username", vbInformation, "Username Required"
Me.txtUsername.SetFocus

ElseIf IsNull(Me.txtPassword) Then
MsgBox "Please enter Password", vbInformation, "Password Required"
Me.txtPassword.SetFocus
Else
'process the job
If (IsNull(DLookup("Username", "tblEmployees", "Username='" & Me.txtUsername.Value & " ' "))) Or _
(IsNull(DLookup("Password", "tblEmployees", "Password='" & Me.txtPassword.Value & " ' "))) Then
MsgBox "Incorrect Login or Password, please contact Administrator"
Else
UserLevel = DLookup("UserSecurity", "tblUsers", "Userlogin='" & Me.txtUsername.Value & " ' ")
DoCmd.Close
If UserLevel = 1 Or UserLevel = 2 Then
MsgBox "Login ID and Password Correct"
DoCmd.OpenForm "frmMenu"
Else
DoCmd.OpenForm "frm_HospitalRptMenu"
End If
End If
End If
Globals.Logging "Logon"
End Sub

jonh
05-10-2016, 08:22 AM
Add a new field to tblUser

DoCmd.OpenForm DLookup("UserForm", "tblUsers", "Userlogin='" & Me.txtUsername.Value & " ' ")

emmorel
05-17-2016, 08:21 AM
Add a new field to tblUser

DoCmd.OpenForm DLookup("UserForm", "tblUsers", "Userlogin='" & Me.txtUsername.Value & " ' ")


jonh, thanks for your feedback; I tried this code at it worked. Now each username that logs into my database opens up a form that is associated to their username. This was very helpful!!

emmorel