Go Back   Access World Forums > Microsoft Access Discussion > Modules & VBA

 
Reply
 
Thread Tools Rate Thread Display Modes
Old 07-19-2019, 05:02 AM   #1
SetanPutih
Newly Registered User
 
Join Date: Jun 2019
Posts: 26
Thanks: 14
Thanked 0 Times in 0 Posts
SetanPutih is on a distinguished road
Open Admin Form after Login

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

SetanPutih is offline   Reply With Quote
Old 07-19-2019, 05:13 AM   #2
CJ_London
Super Moderator
 
Join Date: Feb 2013
Location: UK
Posts: 11,350
Thanks: 40
Thanked 3,671 Times in 3,539 Posts
CJ_London is a glorious beacon of light CJ_London is a glorious beacon of light CJ_London is a glorious beacon of light CJ_London is a glorious beacon of light CJ_London is a glorious beacon of light
Re: Open Admin Form after Login

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
__________________
CJ_London
_______________________
A little thanks goes a long way. If you have found this post useful, please tick the thanks button
CJ_London is offline   Reply With Quote
Old 07-19-2019, 05:19 AM   #3
arnelgp
error reading drive A:
 
arnelgp's Avatar
 
Join Date: May 2009
Location: somewhere out there
Posts: 8,569
Thanks: 68
Thanked 2,745 Times in 2,630 Posts
arnelgp is just really nice arnelgp is just really nice arnelgp is just really nice arnelgp is just really nice arnelgp is just really nice
Re: Open Admin Form after Login

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

__________________
"Never stop learning, because life never stops teaching"
arnelgp is offline   Reply With Quote
The Following User Says Thank You to arnelgp For This Useful Post:
SetanPutih (07-19-2019)
Old 07-19-2019, 05:30 AM   #4
DBQueen
Newly Registered User
 
Join Date: Jul 2017
Posts: 6
Thanks: 13
Thanked 1 Time in 1 Post
DBQueen is on a distinguished road
Re: Open Admin Form after Login

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
DBQueen is offline   Reply With Quote
The Following User Says Thank You to DBQueen For This Useful Post:
SetanPutih (07-19-2019)
Old 07-19-2019, 05:51 AM   #5
SetanPutih
Newly Registered User
 
Join Date: Jun 2019
Posts: 26
Thanks: 14
Thanked 0 Times in 0 Posts
SetanPutih is on a distinguished road
Re: Open Admin Form after Login

Hi Arnel GP,

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

Thanks
Attached Images
File Type: png 2019-07-19 (3).png (98.8 KB, 11 views)
SetanPutih is offline   Reply With Quote
Old 07-19-2019, 06:02 AM   #6
arnelgp
error reading drive A:
 
arnelgp's Avatar
 
Join Date: May 2009
Location: somewhere out there
Posts: 8,569
Thanks: 68
Thanked 2,745 Times in 2,630 Posts
arnelgp is just really nice arnelgp is just really nice arnelgp is just really nice arnelgp is just really nice arnelgp is just really nice
Re: Open Admin Form after Login

its not complete:
Code:
	If cboUser.Column(1) = "Admin" Then
__________________
"Never stop learning, because life never stops teaching"
arnelgp is offline   Reply With Quote
The Following User Says Thank You to arnelgp For This Useful Post:
SetanPutih (07-19-2019)
Old 07-19-2019, 06:10 AM   #7
SetanPutih
Newly Registered User
 
Join Date: Jun 2019
Posts: 26
Thanks: 14
Thanked 0 Times in 0 Posts
SetanPutih is on a distinguished road
Re: Open Admin Form after Login

I'm sorry Arnel. It's still not working. I appreciate the help as always.
Attached Images
File Type: png pass.png (99.9 KB, 10 views)

SetanPutih is offline   Reply With Quote
Old 07-19-2019, 06:30 AM   #8
arnelgp
error reading drive A:
 
arnelgp's Avatar
 
Join Date: May 2009
Location: somewhere out there
Posts: 8,569
Thanks: 68
Thanked 2,745 Times in 2,630 Posts
arnelgp is just really nice arnelgp is just really nice arnelgp is just really nice arnelgp is just really nice arnelgp is just really nice
Re: Open Admin Form after Login

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
__________________
"Never stop learning, because life never stops teaching"
arnelgp is offline   Reply With Quote
Old 07-19-2019, 06:37 AM   #9
SetanPutih
Newly Registered User
 
Join Date: Jun 2019
Posts: 26
Thanks: 14
Thanked 0 Times in 0 Posts
SetanPutih is on a distinguished road
Re: Open Admin Form after Login

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 is offline   Reply With Quote
Old 07-19-2019, 06:48 AM   #10
SetanPutih
Newly Registered User
 
Join Date: Jun 2019
Posts: 26
Thanks: 14
Thanked 0 Times in 0 Posts
SetanPutih is on a distinguished road
Re: Open Admin Form after Login

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.
Attached Images
File Type: png safe.png (93.2 KB, 9 views)
SetanPutih is offline   Reply With Quote
Old 07-19-2019, 06:55 AM   #11
arnelgp
error reading drive A:
 
arnelgp's Avatar
 
Join Date: May 2009
Location: somewhere out there
Posts: 8,569
Thanks: 68
Thanked 2,745 Times in 2,630 Posts
arnelgp is just really nice arnelgp is just really nice arnelgp is just really nice arnelgp is just really nice arnelgp is just really nice
Re: Open Admin Form after Login

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
__________________
"Never stop learning, because life never stops teaching"
arnelgp is offline   Reply With Quote
Old 07-19-2019, 07:10 AM   #12
SetanPutih
Newly Registered User
 
Join Date: Jun 2019
Posts: 26
Thanks: 14
Thanked 0 Times in 0 Posts
SetanPutih is on a distinguished road
Re: Open Admin Form after Login

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.
Attached Images
File Type: jpg 3.jpg (96.8 KB, 8 views)
File Type: jpg 4.jpg (89.4 KB, 9 views)
File Type: png 5.png (93.2 KB, 9 views)
SetanPutih is offline   Reply With Quote
Old 07-19-2019, 07:58 AM   #13
arnelgp
error reading drive A:
 
arnelgp's Avatar
 
Join Date: May 2009
Location: somewhere out there
Posts: 8,569
Thanks: 68
Thanked 2,745 Times in 2,630 Posts
arnelgp is just really nice arnelgp is just really nice arnelgp is just really nice arnelgp is just really nice arnelgp is just really nice
Re: Open Admin Form after Login

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
__________________
"Never stop learning, because life never stops teaching"
arnelgp is offline   Reply With Quote
Old 07-24-2019, 03:02 AM   #14
SetanPutih
Newly Registered User
 
Join Date: Jun 2019
Posts: 26
Thanks: 14
Thanked 0 Times in 0 Posts
SetanPutih is on a distinguished road
Re: Open Admin Form after Login

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
Attached Images
File Type: jpg 1.jpg (102.6 KB, 9 views)
File Type: jpg 2.jpg (93.0 KB, 8 views)
File Type: jpg 3.jpg (90.8 KB, 14 views)
SetanPutih is offline   Reply With Quote
Old 07-24-2019, 08:39 AM   #15
NearImpossible
Newly Registered User
 
Join Date: Jul 2019
Posts: 108
Thanks: 66
Thanked 1 Time in 1 Post
NearImpossible is on a distinguished road
Re: Open Admin Form after Login

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

NearImpossible is offline   Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Trying to add admin rights function in the login form hsusmani Forms 7 02-04-2016 10:42 PM
I need to separate Users and admin on login koketsomaseko Modules & VBA 8 07-11-2014 06:03 AM
Wanted to update my login script and incl admin ct2013 Modules & VBA 1 07-02-2013 02:40 PM
Creating a Login with admin rights eolson30 Theory and practice of database design 1 07-10-2012 11:02 AM
Setting up pw for Admin, but no login for general users? simpleton General 5 10-06-2008 06:18 AM




All times are GMT -8. The time now is 10:28 AM.


Microsoft Access Help
General
Tables
Queries
Forms
Reports
Macros
Modules & VBA
Theory & Practice
Access FAQs
Code Repository
Sample Databases
Video Tutorials

Featured Forum post


Sponsored Links


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
(c) copyright 2017 Access World