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

Disable/Enable Custom Menu Controls

Status
Not open for further replies.

PercyN

Programmer
May 16, 2007
86
Hi, I have a custom menu bar called "MyMenu" which has some drop down menus like "Customers", "Vendors" etc. Under each of these drop down menus, I have a number of controls
For instance under "Customers" I have "New Customer", "New Invoice" etc.

I used the following code

Dim mb As CommandBar
For Each mb in Application.CommandBars
If mb.Name = "MyMenu" Then
mb.Controls("Customers").Enabled = False
End if
Next mb

But what I actually want to do is not disable "Customers" and everything under it but rather to diabale "New Customer" which is a child of "Customers" and leave the other children active. But when I try to refer to the children it does not work.
 
PercyN,
Try this.

Code:
Sub Test_DisableChild()
  Call DisableChild("Customers", "New Customer", False)
End Sub

Code:
Private Const cstrMenu As String = "MyMenu"

Sub DisableChild(Parent As String, Child As String, Enable As Boolean)
  Dim cbr As CommandBar
  Dim cbcParent As CommandBarControl
  Dim cbcChildren As CommandBarControl
  Set cbr = CommandBars(cstrMenu)
  For Each cbcParent In cbr.Controls
    If cbcParent.Caption = Parent Then
      For Each cbcChildren In cbcParent.Controls
        If cbcChildren.Caption = Child Then
          cbcChildren.Enabled = Enable
        End If
      Next cbcChildren
    End If
  Next cbcParent
  
  Set cbcChildren = Nothing
  Set cbcParent = Nothing
  Set cbr = Nothing
End Sub

Sub DrawCommandBar()
  'This is the code I used to replicate your CommandBar
  Dim cbr As CommandBar
  Dim cbp As CommandBarPopup
  Dim cbb As CommandBarButton
  
  Set cbr = Application.CommandBars.Add(cstrMenu)
  cbr.Visible = True
  Set cbp = cbr.Controls.Add(msoControlPopup)
  cbp.Caption = "Customers"
  Set cbb = cbp.Controls.Add(msoControlButton)
  cbb.Caption = "New Customer"
  Set cbb = Nothing
  Set cbb = cbp.Controls.Add(msoControlButton)
  cbb.Caption = "New Invoice"
  Set cbb = Nothing
  
  Set cbp = Nothing
  Set cbr = Nothing
End Sub

Hope this helps,
CMP

[small]For the best results do what I'm thinking, not what I'm saying.[/small]
(GMT-07:00) Mountain Time (US & Canada)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top