Disenabling buttons base on User

G

Guest

Hello I want to open a form and enable or disenable the buttons base on what
type of user is login.

I have a login form that goes to two different switchboard forms base on the
type of user (manage or employee). But both switchboard calls on some of the
same form; like my states form. This form has the buttons of Add, Update,
Delete, and so on. The manager would have the right to do all of the
functions but I want the employees to only have the right to just Update. So
if an employee login and opening the states form I want the Add and Delete
button to be disenable and if a manage login all the buttons stay enable.

So I would like to know does anyone know how to design is code because I am
having some problems designing it.

Thank you,
 
R

Rob Parker

Since you can already determine whether the type of user, I'll assume that
you have a function (IsManager in the following lines) that returns True if
the user is a manager.

In the Open event of your form(s), you can enable/disable various controls
as follows:

Me.MyAddButtonName.Enabled = IsManager

You can also set the form properties to allow edits/additions/deletes in the
same fashion:

Me.AllowEdits = IsManager

HTH,

Rob
 
G

Guest

Hi Rob thanks for replying but Im still a little lost. Yos said I should have
a IsManager function but I didnt have one. So I designing it right now but I
dont really know what I should include.
 
R

Rob Parker

In your first post, you said " ... goes to two different switchboard forms
base on the type of user (manage or employee)." Therefore, you already have
some means of identifying which type of user is logged in. How are you
doing that? Please post the code you are using for that, and we'll go from
there.

Rob
 
G

Guest

I have a user type table thats keeps the usertypeId & usertypename. I have
another that call login which links the userID & the usertypeID together. So
in the form login the code I use is .....

Sub procLogin(UserName As String, Pwd As String)
On Error GoTo LoginErrors

' Declare variables
Dim dbsConnection As Database
Dim rsUser As Recordset
Dim dbstring As String
Dim sqlString As String
Dim thisUser As String
Dim temp As Integer

' DFD 1.3 (DBU1)
' Connect to database by calling a module procedure.
Set dbsConnection = procConnectToDatabase()

' Define and open the table
sqlString = "tblLogin"
Set rsUser = dbsConnection.OpenRecordset(sqlString)

sqlString = "SELECT tblLogin.UserID, tblLogin.Username,
tblLogin.Password, tblLogin.UserTypeID" & _
" FROM tblLogin" & _
" WHERE (((tblLogin.Username)='" & UserName & "') AND
((tblLogin.Password)='" & Pwd & "'));"



' Send the SQL Query to the database connection
Set rsUser = dbsConnection.OpenRecordset(sqlString)

' if eof then user was found with that username/ password
If rsUser.EOF Then
temp = MsgBox("Invalid Login.", vbExclamation, "Can't Log In")
' DFD 1.5
' close recordsets and database connection
rsUser.Close
'Call procCloseDatabase(dbsConnection)
Exit Sub

'DFD 1.6
Else
' User found so find out what type they are and display appropriate GUI
UserTypeID = rsUser!UserTypeID

' Close Login GUI
DoCmd.Close

Select Case UserTypeID

Case 1: ' Manager logged in, open Manager Switchboard GUI
DoCmd.OpenForm "frmManagerSwitchboard", , , , , , rsUser!userID
Case 2: ' Employee switchboard Logged In, open Employee switchboard GUI
DoCmd.OpenForm "frmEmployeeSwitchboard", , , , , , rsUser!userID
Case 3: ' Customer switchboard Logged In, open Customer switchboard GUI
DoCmd.OpenForm "frmCustomerSwitchboard", , , , , , rsUser!userID
Case Else: procDisplayMessage ("You are not authorized to login")

End Select


so now after the user is login a switchboard display depending the type of
user and in this switchboard I have buttons that opening up other forms. Now
the manager switcbhoard and the employee switchboard is different but the 2
have a button that opens up the form states...now heres where I get lost. I
dont know how to enable or disenable the buttons base on the user.
 
R

Rob Parker

There are several possible approaches to this problem. The best, or
feasible, one will depend on how the application works after the switchboard
form is opened, and perhaps on whether the application is split, with each
user having their own front-end and a shared back-end datafile. [Note: I'm
assuming from your current code that you are not using Access's built-in
user-level security.]

First off, I suggest using a function based on whether the manager
switchboard was opened, which will work providing the switchboard form is
not closed until the user ends the session. The switchboard can be hidden
when other forms are opened form it (that's a very common practice), and
this will still work. Copy the following into a module, and save the
module - you must give it a different name to that of this function!

Public Function IsManager() As Boolean
Dim i As Integer
IsManager = False
For i = 0 To Forms().Count - 1
If Forms(i).Name = "frmManagerSwitchboard" Then IsManager = True
Next i
End Function

This returns True or False, depending on whether the frmManagerSwitchboard
is open or not; it uses the fact that a form which is not open is not a
member of the Forms collection. This function will allow the statements I
posted previously to work.

If this doesn't work, post again and we'll explore further alternatives.

HTH,

Rob
 
G

Guest

Thanks Rob I will work on it and let you know if this helps me out.

Rob Parker said:
There are several possible approaches to this problem. The best, or
feasible, one will depend on how the application works after the switchboard
form is opened, and perhaps on whether the application is split, with each
user having their own front-end and a shared back-end datafile. [Note: I'm
assuming from your current code that you are not using Access's built-in
user-level security.]

First off, I suggest using a function based on whether the manager
switchboard was opened, which will work providing the switchboard form is
not closed until the user ends the session. The switchboard can be hidden
when other forms are opened form it (that's a very common practice), and
this will still work. Copy the following into a module, and save the
module - you must give it a different name to that of this function!

Public Function IsManager() As Boolean
Dim i As Integer
IsManager = False
For i = 0 To Forms().Count - 1
If Forms(i).Name = "frmManagerSwitchboard" Then IsManager = True
Next i
End Function

This returns True or False, depending on whether the frmManagerSwitchboard
is open or not; it uses the fact that a form which is not open is not a
member of the Forms collection. This function will allow the statements I
posted previously to work.

If this doesn't work, post again and we'll explore further alternatives.

HTH,

Rob


dcash45 said:
I have a user type table thats keeps the usertypeId & usertypename. I have
another that call login which links the userID & the usertypeID together.
So
in the form login the code I use is .....

Sub procLogin(UserName As String, Pwd As String)
On Error GoTo LoginErrors

' Declare variables
Dim dbsConnection As Database
Dim rsUser As Recordset
Dim dbstring As String
Dim sqlString As String
Dim thisUser As String
Dim temp As Integer

' DFD 1.3 (DBU1)
' Connect to database by calling a module procedure.
Set dbsConnection = procConnectToDatabase()

' Define and open the table
sqlString = "tblLogin"
Set rsUser = dbsConnection.OpenRecordset(sqlString)

sqlString = "SELECT tblLogin.UserID, tblLogin.Username,
tblLogin.Password, tblLogin.UserTypeID" & _
" FROM tblLogin" & _
" WHERE (((tblLogin.Username)='" & UserName & "') AND
((tblLogin.Password)='" & Pwd & "'));"



' Send the SQL Query to the database connection
Set rsUser = dbsConnection.OpenRecordset(sqlString)

' if eof then user was found with that username/ password
If rsUser.EOF Then
temp = MsgBox("Invalid Login.", vbExclamation, "Can't Log In")
' DFD 1.5
' close recordsets and database connection
rsUser.Close
'Call procCloseDatabase(dbsConnection)
Exit Sub

'DFD 1.6
Else
' User found so find out what type they are and display appropriate GUI
UserTypeID = rsUser!UserTypeID

' Close Login GUI
DoCmd.Close

Select Case UserTypeID

Case 1: ' Manager logged in, open Manager Switchboard GUI
DoCmd.OpenForm "frmManagerSwitchboard", , , , , , rsUser!userID
Case 2: ' Employee switchboard Logged In, open Employee switchboard GUI
DoCmd.OpenForm "frmEmployeeSwitchboard", , , , , ,
rsUser!userID
Case 3: ' Customer switchboard Logged In, open Customer switchboard GUI
DoCmd.OpenForm "frmCustomerSwitchboard", , , , , ,
rsUser!userID
Case Else: procDisplayMessage ("You are not authorized to login")

End Select


so now after the user is login a switchboard display depending the type of
user and in this switchboard I have buttons that opening up other forms.
Now
the manager switcbhoard and the employee switchboard is different but the
2
have a button that opens up the form states...now heres where I get lost.
I
dont know how to enable or disenable the buttons base on the user.
 
G

Guest

Hi from what I can tell it seen to be working but Im still going to test if
fully to make sure it working the may I want it to...But thanks a whole lot...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top