I am planning to set user privilege based on the user’s group after they logon. I need helps to correct my code or approach.
I created a table named TUSERID that consist of three fields: UserID, UserName, Password, and Department.
The database open with logon form. After user select their username, enter password and click the cmdLogin button, the event code check the password and user department then open the Main_0 form.
Depending on the user’s department, I want to show/hide selected buttons in the Main_0 form.
I use the DoCmd.OpenForm line with cmdVendorRpt = True/False to control user’s privilege. cmdVendorRpt is one of the buttons that I want to control user's ability to see.
However, this does not work. The Main_0 form opened with all buttons shown no matter what the user department is.
Please help. Below is my code.
Thanks -
Private Sub cmdLogin_Click()
-
-
Dim MyPassword As String
-
Dim MyGroup As String
-
-
'Check to see if data is entered into the UserName combo box
-
-
If IsNull(Me.cboEmployee) Or Me.cboEmployee = "" Then
-
MsgBox "You must enter a User Name.", vbOKOnly, "Required Data"
-
Me.cboEmployee.SetFocus
-
Exit Sub
-
End If
-
-
'Check to see if data is entered into the password box
-
-
If IsNull(Me.txtPassword) Or Me.txtPassword = "" Then
-
MsgBox "You must enter a Password.", vbOKOnly, "Required Data"
-
Me.txtPassword.SetFocus
-
Exit Sub
-
End If
-
-
'Check value of password in tblEmployees to see if this
-
'matches value chosen in combo box
-
-
MyPassword = DLookup("[Password]", "TUSERID", "[UserID]=[cboEmployee]")
-
MyGroup = DLookup("[Department]", "TUSERID", "[UserID] = [cboEmployee]")
-
-
If txtPassword = MyPassword Then
-
-
DoCmd.Close acForm, "F_Logon", acSaveNo
-
-
If MyGroup = "Admin" Then
-
DoCmd.OpenForm "F_Main_0", , , , , , cmdVendorRpt = True
-
-
Else
-
DoCmd.OpenForm "F_Main_0", , , , , , cmdVendorRpt = False
-
-
End If
-
-
Else
-
MsgBox "Password Invalid. Please Try Again", vbOKOnly, _
-
"Invalid Entry!"
-
Me.txtPassword.SetFocus
-
-
End If
-
-
'If User Enters incorrect password 3 times database will shutdown
-
-
intLogonAttempts = intLogonAttempts + 1
-
If intLogonAttempts > 3 Then
-
MsgBox "You do not have access to this database.Please contact admin.", _
-
vbCritical, "Restricted Access!"
-
Application.Quit
-
End If
-
-
-
End Sub
-
-
Only one question per thread please. Just post your "next question" in a new thread and we will be glad to help.
Back to the original question, I will try to help you out in a way that will fit in with your next question. What would probably be simplest would be to just use MyGroup as the value for the OpenArgs property. That way you can get rid of the If/Then/Else statement. So your lines 32-38 can be replaced with - DoCmd.OpenForm FormName:="F_Main_0", OpenArgs:=MyGroup
Then in F_Main_0's OnLoad event, you would use a Select Case statement to test for the OpenArgs value. - Select Case Me.OpenArgs
-
Case "Admin"
-
cmdVendorRpt.Visible = True
-
-
Case "Other Group Name"
-
cmdVendorRpt.Visible = False
-
-
End Select
The down side of doing things this way is that everything is hard coded for what each group can do and also what groups there are. If you want to add a group for some reason down the road, you need to edit your code to look for a new group name. The up side is that it is probably the simplest method to code.
5 1318
The OpenArgs property doesn't allow you to make changes to the form directly. It only allows you to pass a value to the form. What you need to do is come up with a set of values (such as "Admin"/"Other", "True"/"False", 1/0, etc.) that you can test in F_Main_0's OnLoad event. Based on the value in the OpenArgs property of the form, you can then set your button's visible properties to true or false.
Thanks Seth. Can you provide more hint or code example?
Your answer also inspired me for the next question:
Once user entered ID and password, is there a way that their ID and department (group) become global variance? This way, other forms can easily use these variances for user privilege setting. If this can be done, how and where to set the global variance?
Thanks again.
Only one question per thread please. Just post your "next question" in a new thread and we will be glad to help.
Back to the original question, I will try to help you out in a way that will fit in with your next question. What would probably be simplest would be to just use MyGroup as the value for the OpenArgs property. That way you can get rid of the If/Then/Else statement. So your lines 32-38 can be replaced with - DoCmd.OpenForm FormName:="F_Main_0", OpenArgs:=MyGroup
Then in F_Main_0's OnLoad event, you would use a Select Case statement to test for the OpenArgs value. - Select Case Me.OpenArgs
-
Case "Admin"
-
cmdVendorRpt.Visible = True
-
-
Case "Other Group Name"
-
cmdVendorRpt.Visible = False
-
-
End Select
The down side of doing things this way is that everything is hard coded for what each group can do and also what groups there are. If you want to add a group for some reason down the road, you need to edit your code to look for a new group name. The up side is that it is probably the simplest method to code.
Now I understand the concept of passing value using OpenArg. Thanks!
You mentioned a different way to make it easier down the road when new groups are added without needs of hard coding. Could you let me know the approach or articles that discuss this subject?
I will post a new thread regarding global variance.
Thanks,
Joe
Glad to hear it. Our goal is to help you in a way that helps you learn the topic and not just how to fix a specific problem. That way you can use your knowledge later on.
Glad to be able to help.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Jack Smith |
last post by:
Help needed on this question. Any help is appreciated. Thanks in
advance.
Given a binary string (i.e. a finite sequence of 0's and 1's) we
choose any two digit substring 01 and replace it by a...
|
by: Robert V |
last post by:
Hi all, I could use some help programming on of my Perl script to handle
different submit buttons within the same form. Here is what I have so far.
A user goes to a Web form and inputs some data...
|
by: % =joe % |
last post by:
I cannot get this code to work. Very simple...I have three list
menus. I want to do a check before the form submits to make sure that
the value of the 3 fields is equal to 12.
Here's my...
|
by: TDIOwa |
last post by:
I have a form that has a specific date on it. I want to open another
form that has the Active Control Calendar on it. I want to open this
form to the specific date on the first form. I have...
|
by: cefrancke |
last post by:
Is there a way to list (through VBA code) all the controls on an open
form including subforms, tab controls and all other containers as well.
My specific issue is to find all controls with a...
|
by: chris.thompson13 |
last post by:
Any good advice to help solve this problem would be most welcome.
I can not open a recordset based on a query (called qryNoDateSetYet).
The qryNoDateSetYet uses another query within it called...
|
by: erick-flores |
last post by:
Hello all
Form A (pk) & Form B (fk)
I want to display my Form B empty whenever is the first time, that the
pk form Form A, is enter. I click a button to open Form B. BUT if the
pk from Form A...
|
by: kev |
last post by:
Hi folks,
I have a form for registration (frmRegistration) whereby i have two
buttons. One is Save which saves record using the OnClick property. I
used wizard to create the save button. The...
|
by: vidhyapriya |
last post by:
Hi all
I am developing windows application using vb.net.I want to pass values to open form.I am opening only one form when user click the buttons several times.Useing delegate i am passing...
|
by: deepunarayan |
last post by:
Hi I have Problem in ASP.
I have created a Multi choice Question page in ASP with Submit button.
When I submit my page the User Selected values will be taken to the other page where validation...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
| |