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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to disable/enable menu item based on condition? 1

Status
Not open for further replies.

Marryp

Technical User
May 28, 2001
129
0
0
CA
I have created a menu on my application. One of the items is a list of forms called "Modules". I have to validate if the user has access to each module(8 forms). I may need to disable the menu item. The menu looks like this:

File Modules Window
Print Form1 Help
Preview Form2 About
Close Form3
Exit Form4

You help is greatly appreciated... Thank you!
 
Private Sub Form_Open(Cancel As Integer)

If (Yourcondition) Then
Me.cmdYourButtonName.Enabled = False
Else
Me.cmdYourButtonName.Enabled = True
End If
End Sub
 
Those menu are not command buttons.
And how will I know the name of the menu item? I created my menu using the "Customize" function built-in Access. The name of the menu item e.g. Form1 is how the menu item will be displayed.

Hope you can help me!
 
Ok; now I understand... you just created a menu, and dragged/dropped forms/etc to them.

In each form, you'll need to check if the user should be able to open it... and if not, close the form.

Private Sub Form_Load()

If (yourcondition) Then
DoCmd.Close acForm, "theForm'sName", acSaveNo
End If

End Sub

 
You could also notify the user...

Private Sub Form_Load()

If (yourcondition) Then
MsgBox("Sorry, you are not allowed to use this form")
DoCmd.Close acForm, "theForm'sName", acSaveNo
End If

End Sub

If you need to grab a user's windows username to help set up your permissions, it's pretty easy... Here's some code to do that if you're interested:

Public Declare Function apiGetUserName Lib "advapi32.dll" Alias _
"GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Public Function fOSUserName() As String
' Returns the network login name
Dim lngLen As Long, lngx As Long
Dim strUserName As String
strUserName = String$(254, 0)
lngLen = 255
lngx = apiGetUserName(strUserName, lngLen)
If lngx <> 0 Then
fOSUserName = Left$(strUserName, lngLen - 1)
Else
fOSUserName = &quot;Unknown&quot;
' This should never happen... not using empty value to prevent error (ModifiedBy is required)
End If
End Function



 
Actually, I just want to know how to disable (greyed-out)/enable the menu item.

I have a login window created in PowerBuilder the login and password get pass in the database. Right now, I have a Main Menu like a swithcboard. If the user does not have access to the database, he cannot see the menu from the list box and if he does not have access in some other forms it is not displayed. So this part is working for me. But I would like this Main Menu also in the Menu bar that is the &quot;Modules&quot;. This holds all the forms availbale to the user depending on their access rights. I want to implement the same idea I have in Main Menu. But this time instead of not showing the form I will just disable the Menu item.

I know I have to use function but I don't really know how to do it. Do I have to use For...Loop?
 
Ok... a problem... As far as I know, you can't 'grey out' items on a toolbar. If you want that kind've control, you need to move the 'modules' from the menu bar to a switchboard... with the switchboard wizard, that should only be 5 minutes worth of work.

You could do code to make the toolbar depending on which user is logged in... but in my opinion its much simpler to just use an access form (a switchboard).

hth
James

 
If you have custom menus, I'd create two sets. I often have a &quot;user&quot; and &quot;admin&quot; version of custom menus - the &quot;admin&quot; version has additional menu items. When your user logs on, one of the first things the database would do is check to see what groups they belong to, and present the appropriate custom menu.
 
Can I code the menu on VBA?
If YES how do I do that?
 
this code Enables the Add-Ins Option button on the Tools Menu.

Dim cbr As CommandBar
Dim cbrCtl As CommandBarControl
' Set a reference to the Access menu bar
Set cbr = CommandBars(&quot;Menu Bar&quot;)
Set cbrCtl = cbr.Controls(&quot;Tools&quot;).Controls(&quot;Add-Ins&quot;)
' Enable the control from the Tools menu.
cbrCtl.Enabled = True

To Disable use
cbrCtl.Enabled = False

Where &quot;Tools&quot; is the name of the MenuBar, &quot;Add-Ins&quot; is the Control on the MenuBar that you want to alter.

PaulF
 
Hi Paul,

I need more guidance here....

If you look on the first post of this thread, I have showed the example like this:

File Modules Windows
Print Form1 Help
Preview Form2 About
Close Form3
Exit Form4

The only thing I want to disable is the items from &quot;Modules&quot;. Those items open different forms.
So how do I do that. It seems that the code above disable the whole menu bar.

Thanks for your help
happy.gif
 
I tried the code but I get an error: &quot;User-defined type not defined&quot;. The CommandBar does not exist. Do I have to turn on a reference?
 
sorry about that....Yes, set a reference to Microsoft Office 8.0 Object Library

PaulF
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top