login, how to send to different forms?

behedwin

Active Member
Joined
Dec 10, 2014
Messages
393
Hey

I want a simple login screen for my access file.

I have it all working with users in a table.

this code works fine
Code:
    If Me.txtPassword.Value = DLookup("Password", "Access_Table", "[User_ID]=" & Me.txtUsername.Value) Then
 
        lngMyEmpID = Me.txtUsername.Value

'Close logon form and open splash screen
        
        DoCmd.Close acForm, "Login_Form", acSaveNo
        DoCmd.OpenForm "Main_Form"

        Else
        MsgBox "Password Invalid.  Please Try Again", vbOKOnly, "Invalid Entry!"
        Me.txtPassword.SetFocus
    End If


but i want to add another "if" statement if the login is correct.
I have a column in my access_table (where user info is stored) that is named "Role"

I want to send the user to different forms if he types in correct username and password.
If Role = Admin
go to xxxx form
If Role = User
go to yyyy form

how to ad this to my code?
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.

Micron

Well-known Member
Joined
Jun 3, 2015
Messages
2,233
Office Version
  1. 365
Platform
  1. Windows
DLookup the role. How to use depends on what the possibilities are. If many, assign to a variable and write a bunch of If blocks or a Select Case block.
Or, if only 2 possibilities:
Code:
If DLookup("Role", "Access_Table", "[User_ID]=" & Me.txtUsername.Value) = "User" Then
  DoCmd.OpenForm "yyyy"
Else
  DoCmd.OpenForm "xxxx"
End If

You don't need .Value to know the value of a text or combo box. It's the default property. Also, I try to avoid writing multiple instances of a reference such as Me.txtUsername. 1 or 2, OK. Otherwise,
Code:
Dim lngUsrID As Long <-looks to me like it should be a number, not text
lngUsrID = Me.txtUsername <- but the control name suggests it's text??

If DLookup("Role", "Access_Table", "[User_ID]=" & lngUserName)...
None of the above, nor your code, allows for the possibility that DLookup returns nothing
 
Last edited:

Forum statistics

Threads
1,141,046
Messages
5,703,925
Members
421,321
Latest member
blusky4

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top