Dim or Disable specific sub-menu items using VBA?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Aaitaman Tamang
    New Member
    • Jul 2011
    • 22

    Dim or Disable specific sub-menu items using VBA?

    NeaPa,

    Following code in used for disable sub-menu items for specified user. The problem I am having is one of below user is not able see it (Mean disabled the sub-menu items) I want both of them to see sub-menu when they currently on database. Any there is anything wrong with this code which causing one user unable to use sub-menu items.

    Code:
    Dim blnShow As Boolean
         
        blnShow = Nz(StrComp(Me.MyName, "aman", vbTextCompare), True) = False
        With CommandBars("MyMenu").Controls(1)
            .Controls(1).Enabled = blnShow
            .Controls(2).Enabled = blnShow
            .Controls(3).Enabled = blnShow
        End With
        
        blnShow = Nz(StrComp(Me.MyName, "Ram", vbTextCompare), True) = False
            With CommandBars("MyMenu").Controls(1)
            .Controls(1).Enabled = blnShow
            .Controls(2).Enabled = blnShow
            .Controls(3).Enabled = blnShow
        End With
    Prompt help appreciated
    Thanks a lot
    Last edited by NeoPa; Oct 31 '11, 02:09 PM. Reason: Added mandatory [CODE] tags for you
  • MikeTheBike
    Recognized Expert Contributor
    • Jun 2007
    • 640

    #2
    Hi

    On the basis that strComp returns 0 (zero) when the match is found (seem the wrong way round to me - never used it before) then for two users as indicated I suggest this

    Code:
    Dim blnShow As Boolean
    
    blnShow = Nz(StrComp(MyName, "aman", vbTextCompare), True) And Nz(StrComp("Ram", "aman", vbTextCompare), True)
    
    With CommandBars("MyMenu").Controls(1)
        .Controls(1).Enabled = blnShow
        .Controls(2).Enabled = blnShow
        .Controls(3).Enabled = blnShow
    End With
    ??

    For more than two users I would have a lookup table in the back end and use DLookUp(), or more probably DCount() to deal with the Null case, to determine who cannot use the menu item.

    MTB

    Comment

    • Aaitaman Tamang
      New Member
      • Jul 2011
      • 22

      #3
      MTB,

      Thanks a lot for sharing. I have already resolved this task, The following has been used instead of earlier one and working perfect

      Code:
          Select Case Me.MyName
          Case "aman"
              With CommandBars("MyMenu").Controls(1)
                  .Controls(1).Enabled = True
                  .Controls(2).Enabled = False
                  .Controls(3).Enabled = False
              End With
              
              With CommandBars("MyMenu").Controls(2)
                  .Controls(1).Enabled = True
                  .Controls(2).Enabled = True
                  
              End With
          
          Case "Ram"
              With CommandBars("MyMenu").Controls(1)
                  .Controls(1).Enabled = True
                  .Controls(2).Enabled = True
                  .Controls(3).Enabled = False
              End With
          End Select
      Thanks a lot for again sharing
      Last edited by NeoPa; Oct 31 '11, 02:16 PM. Reason: Added mandatory [CODE] tags for you

      Comment

      • NeoPa
        Recognized Expert Moderator MVP
        • Oct 2006
        • 32633

        #4
        If you have two (multiple) users then use InStr() instead :
        Code:
        Dim blnShow As Boolean
         
        blnShow = (InStr(1, ";aman;ram;", ";" & Me.MyName & ";", vbTextCompare) > 0)
        With CommandBars("MyMenu").Controls(1)
            .Controls(1).Enabled = blnShow
            .Controls(2).Enabled = blnShow
            .Controls(3).Enabled = blnShow
        End With
        NB. Don't forget the separators (I used semi-colons (;) in this case) as otherwise you may find a user called "ama" is matched in error.
        Last edited by NeoPa; Oct 31 '11, 02:20 PM. Reason: Changed separator chars to semi-colons (;) to avoid confusion

        Comment

        Working...