Redirect role to specific form

AliBabba

Registered User.
Local time
Yesterday, 22:07
Joined
Nov 6, 2012
Messages
10
Hi,

I've created a log in system with a tutorial I've found on YouTube. It simply redirects users to a specific form after a successful log in. Here is the code:

Code:
Public Sub Login()
        
On Error GoTo ErrorHandler:

    If IsNull([cboUser]) = True Then 'Hier wordt gechecked voor de UserName
        MsgBox "Username is required"
        
    ElseIf IsNull([txtPassword]) = True Then 'Hier wordt gechecked voor de Password
        MsgBox "Password is requ;'/['ired"
        
    Else
    
        'Vergelijk waarden van txtPassword met de bewaarde Password in de tabel tblUsers
        If Me.txtPassword.Value = DLookup("Password", "tblUsers", "[UserName]='" & Me.cboUser.Value & "'") Then
            strUser = Me.cboUser.Value 'Zet de waarde van strUser gedeclareerd als globale variable
            strRole = DLookup("Role", "tblUsers", "[UserName]='" & Me.cboUser.Value & "'") 'Zet de waarde van strRole gedeclareerd als globale variable
            DoCmd.Close acForm, "frmLogin", acSaveNo
            MsgBox "Welcome Back, " & strUser, vbOKOnly, "Welcome"
            DoCmd.OpenForm "frmMenu", acNormal, "", "", , acNormal
            
        Else
            MsgBox "Invalid Password. Please try again.", vbOKOnly, "Invalid Password"
            intLogAttempt = intLogAttempt + 1
            txtPassword.SetFocus
    
        End If
    
    End If
    
    'Hier wordt gekeken of de gebruiker vaker dan 3X foutief probeert in te loggen. In dat geval sluit te applicatie: Application.Quit
    
    If intLogAttempt = 3 Then
        MsgBox "You do not have access to this system." & vbCrLf & vbCrLf & _
        "The system will now shutdown for safety reasons.", vbCritical, "Restricted Access!"
        Application.Quit
    End If
    
ErrorHandler:

End Sub

The users are defined in tabel "tblUsers". All users are now redirected to the form "frmMenu" which is good.

However, I would like to know how to add an exception. For example

1) All users should be redirected to frmMenu after a successful log in. (Check)
2) The user called "Management" should be redirected to an other form "frmMgm"

I've searched on the internet on how to do this but without luck. (forums.asp.net/t/1775325.aspx/1)

So in short:

IF user "Management" logs in THEN redirect to frmMgm (or whatever form)

Thanks in advance, :)
 
Last edited:
Hi,

So I tried a variety of codes but none of them work.

Am I in the right direction with this?

Code:
MsgBox "You have successfully logged in as: " & strUser, vbOKOnly, "Login Successful"
if strUser = "Management" then
DoCmd.OpenForm "frmManagement", acNormal, "", "", , acNormal
Else
DoCmd.OpenForm "frmMenu", acNormal, "", "", , acNormal
End if



edit: Sorry, I was kinda hasty. The following code worked and user "Management" now gets redirected to a specific form


Thanks :)
 
Last edited:
That looks to be the correct approach.

Do you have a user called Management, or is this a Role?

If Management is a role then you need to test strRole rather than strUser.
 
I hope there is no 'user' called "Management", but only Roles.. So you have to use is strRole..
Code:
    MsgBox "You have successfully logged in as: " & strRole, vbOKOnly, "Login Successful"
    If strRole = "Management" then
        DoCmd.OpenForm "frmManagement", acNormal, "", "", , acNormal
    Else
        DoCmd.OpenForm "frmMenu", acNormal, "", "", , acNormal
    End if
 
Hi,

Actually there is a user called "Management" in my case. Is that going to be a problem? It's only used to secure (simple) a switchboard for others.
 
Then all you need to do is change the strRole to strUser...
Code:
Public Sub Login()
    On Error GoTo ErrorHandler:
    If IsNull([cboUser]) = True Then                         
        [COLOR=SeaGreen]'Hier wordt gechecked voor de UserName[/COLOR]
        Call MsgBox("Username is required", vbCritical, "Missing Username")   
    ElseIf IsNull([txtPassword]) = True Then                 
        [COLOR=SeaGreen]'Hier wordt gechecked voor de Password[/COLOR]
        Call MsgBox("Password is required", vbCritical, "Missing Password")   
    Else
        [COLOR=SeaGreen]'Vergelijk waarden van txtPassword met de bewaarde Password in de tabel tblUsers[/COLOR]
        If Me.txtPassword = DLookup("Password", "tblUsers", "[UserName]='" & Me.cboUser & "'") Then
            [COLOR=SeaGreen]'Zet de waarde van strUser gedeclareerd als globale variable[/COLOR]
            strUser = Me.cboUser 
            strRole = DLookup("Role", "tblUsers", "[UserName]='" & Me.cboUser & "'") 'Zet de waarde van strRole gedeclareerd als globale variable
            DoCmd.Close acForm, "frmLogin", acSaveNo
            MsgBox "Welcome Back, " & strUser, vbOKOnly, "Welcome"
            [COLOR=Red]If strUser = "Management" then
                DoCmd.OpenForm "frmManagement", acNormal, "", "", , acNormal
            Else
                DoCmd.OpenForm "frmMenu", acNormal, "", "", , acNormal
            End if[/COLOR]
        Else
            MsgBox "Invalid Password. Please try again.", vbOKOnly, "Invalid Password"
            intLogAttempt = intLogAttempt + 1
            txtPassword.SetFocus
        End If
    End If
    [COLOR=SeaGreen]'Hier wordt gekeken of de gebruiker vaker dan 3X foutief probeert in te loggen. In dat geval sluit te applicatie: Application.Quit[/COLOR]
    If intLogAttempt = 3 Then
        MsgBox "You do not have access to this system." & vbCrLf & vbCrLf & _
        "The system will now shutdown for safety reasons.", vbCritical, "Restricted Access!"
        Application.Quit
    End If

[COLOR=Red]ExitErroHandler:
    Exit Sub
ErrorHandler:
    Call MsgBox(Err.Description, vbCritical, Err.Number)
    Resume ExitErrorHandler:[/COLOR]
End Sub
I have highlighted the portion where I made changes.. I have also removed all trailing .Value as it is not absolutely necessary..

Also make sure the Combo box cboUser is bound to Column 1 and Column 1 is actually a String field not ID..
 
Thank you very much to everyone for their time and effort.

:)
 

Users who are viewing this thread

Back
Top Bottom