[ACCESS] How to create menu silimar the attached photo

How to use vba to create menu when click the button from form in MS Access



Answer this question

[ACCESS] How to create menu silimar the attached photo

  • R2ks

    Hello Perry,

    I did this for a bit of fun and hopefully will help you too. I did this in Excel as I do not have Access at home. I'm sure the basics are the same.

    Create Userform1 with CommandButton1

    Write this in the form code module

    [Form Module Code Start]

    Private Sub CommandButton1_Click()
    CommandBars("MyShortCut").ShowPopup
    End Sub


    Private Sub UserForm_Initialize()
    Call DeleteShortcut ' if "MyShortcut" popup exists then delete it
    Call createShortcut ' add MyShortcut popup
    End Sub

    [Form Module Code End]

    Write this in the Module1 Code Module

    [Module1 Code Start]

    Sub createShortcut()
    Set mybar = CommandBars.Add(Name:="MyShortcut", Position:=msoBarPopup, temporary:=True)
    'add a menu items
    Set myitem = mybar.Controls.Add(Type:=msoControlButton)
    With myitem
    .Caption = "&Add"
    .OnAction = "AddSomeThing"
    End With
    Set myitem = mybar.Controls.Add(Type:=msoControlButton)
    With myitem
    .Caption = "&Remove"
    .OnAction = "RemoveSomeThing"
    End With
    Set myitem = mybar.Controls.Add(Type:=msoControlButton)
    With myitem
    .Caption = "Reset"
    .OnAction = "ResetSomeThing"
    End With
    End Sub

    Sub DeleteShortcut()
    For Each cbar In CommandBars
    If cbar.Name = "MyShortcut" Then
    cbar.Delete
    End If
    Next
    End Sub

    Sub AddSomething()
    MsgBox ("In add something")
    End Sub
    Sub RemoveSomething()
    MsgBox ("In Remove Something")
    End Sub

    Sub ResetSomething()
    MsgBox ("In Reset Something")
    End Sub

    [Module1 Code End]

    The last few procedures MUST be in Module1 (or whatever you wish to call it), they CANNOT be in the Form Code Module.

    Please let me know how you get on with this, it was fun and educational!!

    ChasAA


  • Krutika

    Sorry I forgot to metion that if you want any items disabled, you just need to add:

    Set myitem = mybar.Controls.Add(Type:=msoControlButton)
    With myitem
    .Caption = "&Remove"
    .OnAction = "RemoveSomeThing"
    .Enabled = False
    End With

    This can be turned on or off with a variable in your code.

    ChasAA


  • [ACCESS] How to create menu silimar the attached photo