Open Specific Form Based on Text Entered

DMFisher

New Member
Joined
Apr 22, 2016
Messages
35
I Currently have the following code which is working perfectly... is it now possible to have code that opens a certain form dependent on the username entered?

Public Sub Login()

On Error GoTo ErrorHandler:


If IsNull([cboUser]) = True Then 'Check UserName
MsgBox "Username is required"

ElseIf IsNull([txtPassword]) = True Then 'Check Password
MsgBox "Password is required"

Else

'Compare value of txtPassword with the saved Password in tblUser
If Me.txtPassword.Value = DLookup("Password", "tblUsers", "[UserName]='" & Me.cboUser.Value & "'") Then
strUser = Me.cboUser.Value 'Set the value of strUser declared as Global Variable
strRole = DLookup("Role", "tblUsers", "[UserName]='" & Me.cboUser.Value & "'") 'set the value of strRole declared as Global Variable
DoCmd.Close acForm, "frmLogin", acSaveNo
DoCmd.OpenForm "frmMenu", acNormal, "", "", , acNormal

Else
MsgBox "Invalid Password. Please try again.", vbOKOnly, "Invalid Password"
intLogAttempt = intLogAttempt + 1
txtPassword.SetFocus

End If

End If

'Check if the user has 3 wrong log-in attempts and close the application
If intLogAttempt = 3 Then
MsgBox "You do not have access to this database.Please contact admin." & vbCrLf & vbCrLf & _
"Application will exit.", vbCritical, "Restricted Access!"
Application.Quit
End If

ErrorHandler:


End Sub
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
A couple of things. When posting code you should add
Code:
 at the start of your code and add
at the end.
This will preserve your formatting and make it easier for others to view your code.
I do not think the code you posted is 'working perfectly'.
Code:
     ElseIf IsNull([txtPassword]) = True Then 'Check Password
     MsgBox "Password is required"
you are testing txtPassword to be not null, but the purpose of the function is to get the user to enter it
so that you can validate the user. Therefore your function should fail.
Based upon the code you posted I cannot tell if you are using option explicit, if you are not you should be.
Code:
Option Compare Database
Option Explicit
To set this to be the default got to tools >>options>>editor tab and make sure Require Variable Declaration is checked.
I would not show all the users (cboUser) rather I would have the user enter their user name in a textbox.
Then test for the presence of a user name using
Code:
     if len(me.txtUser & "") = 0 then
         msgbox "User name is required.",vbokonly,"Missing data."
         me.txtUserName.setfocus
         exit sub
     end if
.
This tests for Null and empty strings.
If you are using global variables it helps to prefix the variable name with g.. , e.g. gstrUser.
To answer you overall question of users having different forms you could add a column frmLogin and populate the table
with the appropriate form name.

hth Jack
 
Upvote 0

Forum statistics

Threads
1,223,246
Messages
6,170,996
Members
452,373
Latest member
TimReeks

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
Back
Top