Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Limit menu selections on MDI form, for a group of users 3

Status
Not open for further replies.

JerryKlmns

IS-IT--Management
Feb 7, 2005
2,062
GR
Kalimera,

I have a program that connects to mdb's, using user level security (with .mdw). I have created a menu for the MDI form. I want for a group of users, not to be able to use a part of the menu. Like File-->New, Open, [gray]Edit[/gray] etc.

How could I accomplish this?
 
If I understand correctly, you want certain menu items disabled depending on the user that logged in. When you use VB's menu editor, you give each menu item a name (for example, mnuNew, mnuOpen, mnuEdit, etc...)

You can simply disable the menu items like this

Private Sub MDIForm_Load()

If UserType <> 'Administrator' Then
mnuEdit.Enabled = False
End If

End Sub

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
And so it is not hardcoded like the example above I would suggest creating a table with users and menu names and load this to decide which menus are (un)available to each user.

Regards

Frederico Fonseca
SysSoft Integrated Ltd
 
gmmastros: what I needed

Frederico: muchísimas gracias, for that hint! I shall get it to work right now!!
(haven 't found any Portuguese dict, hope spanish works for you as well)

Both responces are hailed as positive steps towards the solution !
* 4 U both
 
There is also a .Visible property that you could use. Depending on the situation, you may not want certain people to even see menu items.

For example, View -> Employee -> Salaries



-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
With the following menu structure:
mnuFile
..mnuNew
..mnuOpen
..mnuSave
mnuEdit
..mnuCopy
..mnuPaste
mnuAdmin
..mnuAdTools
..mnuAdReports


Try this:
Code:
Dim UserType As String

Private Sub Form_Load()
  Dim Admin As String, User1 As String, User2 As String
  Admin = "mnuFile,mnuNew,mnuOpen,mnuSave,mnuEdit,mnuCopy,mnuPaste,mnuAdmin,mnuAdTools,mnuAdReports"
  User1 = "mnuFile,mnuNew,mnuOpen,mnuSave,mnuEdit,mnuCopy,mnuPaste"
  User2 = "mnuFile,mnuOpen"
  
  UserType = "Admin"
  'UserType = "User1"
  'UserType = "User2"
  
  Dim Menus As String
  Select Case UserType
    Case "Admin": HideMenus Admin
    Case "User1": HideMenus User1
    Case "User2": HideMenus User2
  End Select
End Sub

Private Sub HideMenus(Menus As String)
  On Error Resume Next
    Dim m As Menu, i As Integer
    For Each m In Me.Controls
      m.Visible = InStr(1, Menus, m.Name)
    Next
  On Error GoTo 0
End Sub

Visit My Site
PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Here is a better example, same as above with 3 CommandButtons:
Code:
Dim Admin As String, User1 As String, User2 As String

Private Sub Command1_Click()
  HideMenus Admin, Me
End Sub

Private Sub Command2_Click()
  HideMenus User1, Me
End Sub

Private Sub Command3_Click()
  HideMenus User2, Me
End Sub

Private Sub Form_Load()
  Admin = "mnuFile,mnuNew,mnuOpen,mnuSave,mnuEdit,mnuCopy,mnuPaste,mnuAdmin,mnuAdTools,mnuAdReports"
  User1 = "mnuFile,mnuNew,mnuOpen,mnuSave,mnuEdit,mnuCopy,mnuPaste"
  User2 = "mnuFile,mnuOpen"
  
  HideMenus User1, Me
  
  Command1.Caption = "Admin"
  Command2.Caption = "User1"
  Command3.Caption = "User2"
End Sub

Private Sub HideMenus(Menus As String, f As Form)
  On Error Resume Next
    Dim m As Control
    For Each m In f.Controls
      If InStr(1, m.Name, "mnu") Then m.Visible = InStr(1, Menus, m.Name)
    Next
  On Error GoTo 0
End Sub

Visit My Site
PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
CubeE101

I was to build that logic today and just saw your answer.
I have to thank you too. May be more than just a thanx, since you spent time to write extensive code for me.

I owe you a *

Gerrasimos
 

I think, my opinion of course, that assigning the users to an edit level, and then based on their edit level, show/hide the menus. That way you do not have to do a hard code of the user names/groups.
Here is a simple schema:

1. Get from db the UserEditLevel for the logged on user.
UserEditLevelID = 2

2.
Select case True
Case UserEditLevelID = 0
'Hide/Show menu list

Case UserEditLevelID = 1
'Hide/Show menu list

Case UserEditLevelID = 2
'Hide/Show menu list

End Select

I have done a db table where all important menu items are represented there, and the admin can assign a certain edit level for each user:

If 0 then they see not the menu point
If 1 then they see it but can not edit
If 2 then they see, and can edit it

The when your app. starts you only need to get the user log in ID, and take that to the db to extract which menu points to show. If they use the menu item and open it, then based on the edit level for that form and user, the user can either just read the data, or also edit it.
 
Thanks LostInCode. I 'll try to parameterized it to the max I can think, since it 's my first to build. When done I 'll share it with the rest of us.

Who 's gonna be lost in code, arround here??????
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top