Login Screen on forum (1 Viewer)

stu_c

Registered User.
Local time
Today, 17:21
Joined
Sep 20, 2007
Messages
489
Hi All
I have got some code for a form login screen, I am trying to add multiple account types but really struggling any suggestions would be great I would like for example
Admin
Standard
ViewOnly
FinanceOnly


Code:
Private Sub frm1Btn_Click()
If IsNull(Me.tb_ID) Or IsNull(Me.tb_pwd) Then
MsgBox "You must enter password and login ID.", vbOKOnly + vbInformation, "Required Data"
Me.tb_ID.SetFocus
Exit Sub
End If

If Me.tb_pwd.Value = DLookup("[Login_Pwd]", "login_tbl", "[Empl ID]='" & Me.tb_ID.Value & "'") Then
If DLookup("[User_Type]", "login_tbl", "[Empl ID]='" & Me.tb_ID.Value & "'") = "Admin" Then
DoCmd.openForm "admin_menu"
DoCmd.Close acForm, "login", acSaveNo
Else
DoCmd.openForm "user_menu"
DoCmd.Close acForm, "login", acSaveNo
End If
Else
MsgBox "Password or login ID incorrect. Please Try Again", vbOKOnly + vbExclamation, "Invalid Entry!"
Me.tb_pwd.SetFocus
End If
End Sub
 

theDBguy

I’m here to help
Staff member
Local time
Today, 09:21
Joined
Oct 29, 2018
Messages
21,467
Hi. I thought you were talking about the login page on this website. Typically, you would have a table listing all the authorized users. If so, that table should have a primary key. In your login form, you can retrieve this ID information and then use it to check what level the user is assigned to. You could then use a Select Case statement to open the correct form. For example:
Code:
UserID = DLookup("UserID"...)
UserLevel = DLookup("UserLevel",...,"UserID=" & UserID)
Select Case UserLevel
    Case "Admin"
        'open admin form
    Case "User"
        'open user form
    Case "OtherLevel"
        'do stuff
End Select
Hope that helps...
 

stu_c

Registered User.
Local time
Today, 17:21
Joined
Sep 20, 2007
Messages
489
Hi mate
Thank you for the quick response, is there anything I can add to my original code for it to work?


Hi. I thought you were talking about the login page on this website. Typically, you would have a table listing all the authorized users. If so, that table should have a primary key. In your login form, you can retrieve this ID information and then use it to check what level the user is assigned to. You could then use a Select Case statement to open the correct form. For example:
Code:
UserID = DLookup("UserID"...)
UserLevel = DLookup("UserLevel",...,"UserID=" & UserID)
Select Case UserLevel
    Case "Admin"
        'open admin form
    Case "User"
        'open user form
    Case "OtherLevel"
        'do stuff
End Select
Hope that helps...
 

theDBguy

I’m here to help
Staff member
Local time
Today, 09:21
Joined
Oct 29, 2018
Messages
21,467
Hi mate
Thank you for the quick response, is there anything I can add to my original code for it to work?
Hi. Probably, but I can't tell you what exactly if I don't understand what you're trying to do. Can you post a sample copy of your db?
 

stu_c

Registered User.
Local time
Today, 17:21
Joined
Sep 20, 2007
Messages
489
Basically I have it set like this this

TABLE (login_tbl)
Forename
Surname
Empl_ID
Login_Pwd
User_Type (Select from: Admin; Standard; View Only; Finance Only)

FORM (frm_Login)
tb_ID - For Empl_ID (In Table)
tb_pwd - For Login_Pwd (In Table)
frm1Btn - Login Button

what I want is when the User_Type has been checked it takes the user to the correct for depending on their account type, The below code works fine for two accounts but struggling for multiple accounts.

Code:
Private Sub frm1Btn_Click()
If IsNull(Me.tb_ID) Or IsNull(Me.tb_pwd) Then
MsgBox "You must enter password and login ID.", vbOKOnly + vbInformation, "Required Data"
Me.tb_ID.SetFocus
Exit Sub
End If

If Me.tb_pwd.Value = DLookup("[Login_Pwd]", "login_tbl", "[Empl ID]='" & Me.tb_ID.Value & "'") Then
If DLookup("[User_Type]", "login_tbl", "[Empl ID]='" & Me.tb_ID.Value & "'") = "Admin" Then
DoCmd.openForm "admin_menu"
DoCmd.Close acForm, "login", acSaveNo
Else
DoCmd.openForm "user_menu"
DoCmd.Close acForm, "login", acSaveNo
End If
Else
MsgBox "Password or login ID incorrect. Please Try Again", vbOKOnly + vbExclamation, "Invalid Entry!"
Me.tb_pwd.SetFocus
End If
End Sub
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 00:21
Joined
May 7, 2009
Messages
19,230
Code:
Private Sub frm1Btn_Click()
    
    Dim UserType As String
    
    If IsNull(Me.tb_ID) Or IsNull(Me.tb_pwd) Then
        MsgBox "You must enter password and login ID.", vbOKOnly + vbInformation, "Required Data"
        Me.tb_ID.SetFocus
        Exit Sub
    End If
    
    UserType = DLookup("[User_Type]", "login_tbl", "[Empl_ID]='" & Me.tb_ID.value & "' And Login_Pwd='" & Me.tb_pwd & "'") & ""
    
    Select Case UserType
        Case "Admin"
            DoCmd.OpenForm "admin_menu"
            DoCmd.Close acForm, "login", acSaveNo
        
        Case "Standard"
            DoCmd.OpenForm "open what form"
            DoCmd.Close acForm, "login", acSaveNo
            
        Case "View Only"
            DoCmd.OpenForm "open what form"
            DoCmd.Close acForm, "login", acSaveNo
    
        Case "Finance Only"
            DoCmd.OpenForm "open what form"
            DoCmd.Close acForm, "login", acSaveNo
        
        Case Else
            MsgBox "Password or login ID incorrect. Please Try Again", vbOKOnly + vbExclamation, "Invalid Entry!"
            Me.tb_pwd.SetFocus
    End Select
End Sub
 

stu_c

Registered User.
Local time
Today, 17:21
Joined
Sep 20, 2007
Messages
489
Thank you so much for the code!
for some strange reason it now says the password or ID is incorrect?

Code:
Private Sub frm1Btn_Click()
    
    Dim UserType As String
    
    If IsNull(Me.tb_ID) Or IsNull(Me.tb_pwd) Then
        MsgBox "You must enter password and login ID.", vbOKOnly + vbInformation, "Required Data"
        Me.tb_ID.SetFocus
        Exit Sub
    End If
    
    UserType = DLookup("[User_Type]", "login_tbl", "[Empl_ID]='" & Me.tb_ID.value & "' And Login_Pwd='" & Me.tb_pwd & "'") & ""
    
    Select Case UserType
        Case "Admin"
            DoCmd.OpenForm "admin_menu"
            DoCmd.Close acForm, "login", acSaveNo
        
        Case "Standard"
            DoCmd.OpenForm "open what form"
            DoCmd.Close acForm, "login", acSaveNo
            
        Case "View Only"
            DoCmd.OpenForm "open what form"
            DoCmd.Close acForm, "login", acSaveNo
    
        Case "Finance Only"
            DoCmd.OpenForm "open what form"
            DoCmd.Close acForm, "login", acSaveNo
        
        Case Else
            MsgBox "Password or login ID incorrect. Please Try Again", vbOKOnly + vbExclamation, "Invalid Entry!"
            Me.tb_pwd.SetFocus
    End Select
End Sub
 

isladogs

MVP / VIP
Local time
Today, 17:21
Joined
Jan 14, 2017
Messages
18,211
After the DLookup line add this
Code:
Debug.Print "User Type = " & UserType.
See what it shows in the Immediate window
I suspect it will be an empty string so will just show User Type =
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 00:21
Joined
May 7, 2009
Messages
19,230
you need to debug, your code.
add Option Explicit as the second line of your form module.
click your mouse at "Private Sub frm1Btn_Click()"
and press F9.
run your form and enter the username/pwd.
when it returns you back to the frm1Btn_Click, press F8.
inspect each variable by hovering your mouse on each variables as you
press F8 on each line.
 

stu_c

Registered User.
Local time
Today, 17:21
Joined
Sep 20, 2007
Messages
489
HI mate
what do you mean by add Option Explicit as the second line of your form module?

you need to debug, your code.
add Option Explicit as the second line of your form module.
click your mouse at "Private Sub frm1Btn_Click()"
and press F9.
run your form and enter the username/pwd.
when it returns you back to the frm1Btn_Click, press F8.
inspect each variable by hovering your mouse on each variables as you
press F8 on each line.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 00:21
Joined
May 7, 2009
Messages
19,230
if you go to the form VBA (or any other module), you will find this:

Option Compare Database
Option Explicit
 

Users who are viewing this thread

Top Bottom