Open Admin Form after Login (1 Viewer)

SetanPutih

Registered User.
Local time
Today, 02:29
Joined
Jun 27, 2019
Messages
27
Hi,

Hope someone can help with this. I want Admin to be directed to a different form after login. Currently all Employees are directed to Form1. I want Admin to be directed to Main. The login code can be seen below. Can anybody help please?

Private Sub cmdLogin_Click()
Dim strID As String
strID = DLookup("[EmployeeID]", "Employees", "[UserName]='" & Me.cboUser.Column(1) & "" & "' And Password = '" & Me.txtPassword & "" & "'") & ""
If strID <> "" Then
gEmployeeID = strID
Forms!Form1!lblWelcome.Caption = "Welcome " & Me.cboUser.Column(1) & " " & Me.cboUser.Column(2)

DoCmd.Close acForm, Me.Name



Else
MsgBox "Invalid User or Password. Please try again.", vbInformation + vbOKOnly

End If
End Sub

Private Sub Form_Close()
If gEmployeeID = "" Then Application.Quit
End Sub
 

CJ_London

Super Moderator
Staff member
Local time
Today, 09:29
Joined
Feb 19, 2013
Messages
16,553
your login code does not contain an openform command so implies either it does not work at all, or your are opening the form somewhere else

in principle the code would be

Code:
if user=admin then 
    docmd.openform "main"
else
    docmd.openform "form1"
end if
and needs to be run after the user has logged in
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 17:29
Joined
May 7, 2009
Messages
19,174
Code:
Private Sub cmdLogin_Click()
Dim strID As String
strID = DLookup("[EmployeeID]", "Employees", "[UserName]='" & Me.cboUser.Column(1) & "" & "' And Password = '" & Me.txtPassword & "" & "'") & ""
If strID <> "" Then
	gEmployeeID = strID
	If cboUser.Column(1) = "Admin"
		'close Form1
		Docmd.Close acForm, "Form1"
		'Open the Main form
		DoCmd.OpenForm "Admin"
	Else

		Forms!Form1!lblWelcome.Caption = "Welcome " & Me.cboUser.Column(1) & " " & Me.cboUser.Column(2)
	End If

	DoCmd.Close acForm, Me.Name



Else
	MsgBox "Invalid User or Password. Please try again.", vbInformation + vbOKOnly

End If
End Sub

Private Sub Form_Close()
If gEmployeeID = "" Then Application.Quit
End Sub
 

DBQueen

New member
Local time
Today, 20:29
Joined
Jul 13, 2017
Messages
6
Hi SetanPutih,

You have to add another field to your table employee.

admin(Boolean) - additional field

** for real world table, you can have 3 tables - User, Employee, Role

Function IsAdmin(strID As String) As Boolean
Dim blnAns As Boolean

blnAns = DLookup("[admin]","Employees",<your filter>)

If blnAns=Yes then
DoCmd.OpenForm "Main"
Else
DoCmd.OpenForm "Form1"
EndIf


End Function
 

SetanPutih

Registered User.
Local time
Today, 02:29
Joined
Jun 27, 2019
Messages
27
Hi Arnel GP,

Thanks for your help. Unfortunately it didn't work. Please see image below.

Thanks
 

Attachments

  • 2019-07-19 (3).png
    2019-07-19 (3).png
    98.8 KB · Views: 117

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 17:29
Joined
May 7, 2009
Messages
19,174
its not complete:
Code:
	If cboUser.Column(1) = "Admin" [COLOR="Blue"]Then[/COLOR]
 

SetanPutih

Registered User.
Local time
Today, 02:29
Joined
Jun 27, 2019
Messages
27
I'm sorry Arnel. It's still not working. I appreciate the help as always.
 

Attachments

  • pass.png
    pass.png
    99.9 KB · Views: 107

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 17:29
Joined
May 7, 2009
Messages
19,174
Code:
Private Sub cmdLogin_Click()
Dim strID As String
strID = DLookup("[EmployeeID]", "Employees", "[UserName]='" & Me.cboUser.Column(1) & "" & "' And Password = '" & Me.txtPassword & "" & "'") & ""
If strID <> "" Then
	gEmployeeID = strID
	If cboUser.Column(1) = "Admin" Then
		'close Form1
		Docmd.Close acForm, "Form1"
		'Open the Main form
		DoCmd.OpenForm "Admin"
	Else

		Forms!Form1!lblWelcome.Caption = "Welcome " & Me.cboUser.Column(1) & " " & Me.cboUser.Column(2)
	End If

	DoCmd.Close acForm, Me.Name



Else
	MsgBox "Invalid User or Password. Please try again.", vbInformation + vbOKOnly

End If
End Sub
 

SetanPutih

Registered User.
Local time
Today, 02:29
Joined
Jun 27, 2019
Messages
27
Thanks for your reply. Unfortunately it still doesn't work. When I'm on the Login screen I put the name and password it and it doesn't do anything. It just stays on the login screen.
 

SetanPutih

Registered User.
Local time
Today, 02:29
Joined
Jun 27, 2019
Messages
27
Once I put in any login details and password the login screen remains inactive. Once I exit the programme it tells be there's an error. If i got to Debug it pops up with this another error (Please see picture). Thanks again for your time.
 

Attachments

  • safe.png
    safe.png
    93.2 KB · Views: 103

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 17:29
Joined
May 7, 2009
Messages
19,174
where do you call the Login form, from form1?
if so remove the code that closes form1.
Code:
	If cboUser.Column(1) = "Admin" Then
		'Open the Main form
		DoCmd.OpenForm "Admin"
	Else
 

SetanPutih

Registered User.
Local time
Today, 02:29
Joined
Jun 27, 2019
Messages
27
The database in built around the database you sent me (Picture 1). The Login form appears as soon as the database opens. Once the Login form is completed correctly, it directs you to Form1. Currently, if I enter the correct name and password the Login form remains and nothing happens (Picture 2) if i put the login form into design mode it takes me to the End or Debug options. On the debug window it shows Picture 3. Thank you.
 

Attachments

  • 3.jpg
    3.jpg
    96.8 KB · Views: 118
  • 4.jpg
    4.jpg
    89.4 KB · Views: 121
  • 5.png
    5.png
    93.2 KB · Views: 122

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 17:29
Joined
May 7, 2009
Messages
19,174
okey.
add an Unbound textbox to Form1, anywhere in the form.
name it txtHidden.
Set it's Visible Property to No.

add code to the Timer Event:
Code:
Private Sub Form_Timer()
    Me.TimerInterval = 0
    DoCmd.OpenForm FormName:="frmLogIn", windowmode:=acDialog
    If Trim(Me.txtHidden & "") = "Admin" Then
        DoCmd.OpenForm FormName:="yourMainFormNameHere"
        DoCmd.Close acForm, Me.Name
    End If
End Sub

on the Login form, add this changes:
Code:
Private Sub cmdLogin_Click()
Dim strID As String
strID = DLookup("[EmployeeID]", "Employees", "[UserName]='" & Me.cboUser.Column(1) & "" & "' And Password = '" & Me.txtPassword & "" & "'") & ""
If strID <> "" Then
	gEmployeeID = strID
	If cboUser.Column(1) = "Admin" Then
		'set unbound textbox of Form1
		Forms!Form1!txtHidden = "Admin"
	Else

		Forms!Form1!lblWelcome.Caption = "Welcome " & Me.cboUser.Column(1) & " " & Me.cboUser.Column(2)
	End If
 

SetanPutih

Registered User.
Local time
Today, 02:29
Joined
Jun 27, 2019
Messages
27
Hi arnelgp,

Thank you for your input again. I have just tried what you suggested.

1) Add an unbound textbox to Form1, name it txhHidden. Set it's Visible Property to No. (Picture 1)

2)Add code to Timer Event: (Picture 2)

3) On the login form, add this change (Picture 3)

As you can see from Picture 3 I am unable to proceed. What do you suggest?

Thank you as always
 

Attachments

  • 1.jpg
    1.jpg
    102.6 KB · Views: 114
  • 2.jpg
    2.jpg
    93 KB · Views: 112
  • 3.jpg
    3.jpg
    90.8 KB · Views: 126

NearImpossible

Registered User.
Local time
Today, 04:29
Joined
Jul 12, 2019
Messages
225
Per your screen shot #3, You need an End IF above your 2nd Else statement.

This is where indention comes in handy, as shown in arnelgp's code above, to help you make sure you closing each opening, especially in nested code.

if x=x then
if y=y​
do #1​
else​
do #2​
end if​
else
do #3​
end if
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 17:29
Joined
May 7, 2009
Messages
19,174
as suggested, put an End If before the last Else
Code:
End If
Else
    Msgbox "Invalid User or Password....
End If
 

SetanPutih

Registered User.
Local time
Today, 02:29
Joined
Jun 27, 2019
Messages
27
Thank you both for your input. The database is very nearly finished but I'm still having problem with the Admin login.

This is the code for the timer event (picture 1)

This is the code for the login form (picture 2)

The problem is now when I login as admin, nothing happens. The screen just stays the same and the login button doesn't take me anywhere (picture 3)

BUT if I exit the login screen by clicking the Exit Cross in the top right hand corner of the login window, it then directs me to the Admin page where the button is supposed to take me (picture 4)

This will hopefully be the finishing touch to the database! Thank you very much guys.
 

Attachments

  • 1.png
    1.png
    99.1 KB · Views: 108
  • 2.png
    2.png
    92.4 KB · Views: 110
  • 3.jpg
    3.jpg
    91.2 KB · Views: 104
  • 4.jpg
    4.jpg
    88.4 KB · Views: 106

Gasman

Enthusiastic Amateur
Local time
Today, 09:29
Joined
Sep 21, 2011
Messages
14,050
Just walk through the code line by line.

You could also line up your ifs and endifs to make the logic clearer.?
 

SetanPutih

Registered User.
Local time
Today, 02:29
Joined
Jun 27, 2019
Messages
27
Thanks @Gasman,

Unfortunately, I have no idea why it doesn't direct me to "frmPCSManager" once I press the login button but it does when I exit the login window. Even after I've gone through the code line by line. I'm learning a lot about Access every day but I'm just not at the point of being able to alter coding for different outcomes yet. Enjoying the learning journey though!
 

Gasman

Enthusiastic Amateur
Local time
Today, 09:29
Joined
Sep 21, 2011
Messages
14,050
How are you opening the forms?
I would expect a login form, get past that and you open the next form/switchboard?
In your login form if there is a strid and the cbo column is Admin, you set a control.
I do not see how you open any form.

OK, the open form is in Form1, but how are you meant to get to that.?

As I said, if you walk through the code line by line, you should be able to see where it is going wrong and why.

That is always my first port of call for silly errors like this. Then you will also find out why it does get there eventually.

Perhaps identify the steps you think the code should be doing, and then an expert can direct you better.?
 

Users who are viewing this thread

Top Bottom