Go Premium for a chance to win a PS4. Enter to Win



Posted on 2000-02-24
Medium Priority
Last Modified: 2008-02-20
how to create any menu item from code...
not sub menu ....
for example how to create menu item called FILE in next structure with next sub menus from code...


Please provide some samples and examples for this.

Question by:bokikg

Expert Comment

ID: 2553735
'use menueditor create a submenu of mnuFile
caption = "-"
name = "mnuFileSub"
index = 0

Sub CreateSubMenus()
    mnuFileSub(0).Visible = True
    While mnuFileSub.Count > 1
        Unload mnuFileSub(mnuFileSub.Count - 1)
    ' create submenus as you wish
    Load mnuFileSub(1)
    mnuFileSub(1).Caption = "&Open"
    mnuFileSub(1).Visible = True
    mnuFileSub(0).Visible = (mnuFileSub.Count = 1)
End Sub


Author Comment

ID: 2554375
you didn't figer out ... how to make menu item called FILE not submenu... i know to that.... how to create parent menu item not submenu item from code....

Expert Comment

ID: 2556610
so sorry. Now I'm try exactly way, need some time...
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 2556686
Why dont you create the menu whit the editor, make it invisible, and when you want to show it, make it visible ?

Or explain better your wish..

Accepted Solution

Ruchi earned 300 total points
ID: 2556781
From the web:


Option Explicit
Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long

Private Declare Function CreateMenu Lib "user32" () As Long
Private Declare Function AppendMenu Lib "user32" Alias "AppendMenuA" (ByVal hMenu As Long, ByVal wFlags As Long, ByVal wIDNewItem As Long, ByVal lpNewItem As Any) As Long
Private Declare Function SetMenu Lib "user32" (ByVal hwnd As Long, ByVal hMenu As Long) As Long
Private Declare Function DrawMenuBar Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function CreatePopupMenu Lib "user32" () As Long
Public Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Long, ByVal bRevert As Long) As Long

Private Const MF_ENABLED = &H0&
Private Const MF_POPUP = &H10&
Private Const MF_SEPARATOR = &H800&
Private Const MF_STRING = &H0&

Private Const WM_MENUSELECT = &H11F
Public Const GWL_WNDPROC = (-4)

Public lPrevWnd As Long
Public lSysMnu As Long
Public lMnu As Long

Public Sub CreateMenuBar()
    'Generate a Simple File Menu..
    Dim lSubMnu As Long
    Dim lRes As Long
    lMnu = CreateMenu           'Create a Menu Item
    lSubMnu = CreatePopupMenu   'Create a Popup Menu
    'Add Items to the Popup Menu
    'Each Item Requires a Unique ID to Identify it in our Menu Event
    lRes = AppendMenu(lSubMnu, MF_ENABLED Or MF_STRING, 1, ByVal "&Open")
    lRes = AppendMenu(lSubMnu, MF_ENABLED Or MF_STRING, 2, ByVal "&Save")
    lRes = AppendMenu(lSubMnu, MF_ENABLED Or MF_STRING, 3, ByVal "Save &As..")
    lRes = AppendMenu(lSubMnu, MF_ENABLED Or MF_STRING, 4, ByVal "&Exit")
    'Add the Popup Menu to the Main File Menu Item..
    lRes = AppendMenu(lMnu, MF_ENABLED Or MF_STRING Or MF_POPUP, lSubMnu, ByVal "File")
    'Assign the Menu to the Form
    lRes = SetMenu(Form1.hwnd, lMnu)
    'Draw the Menu, Only works when the Form is Visible.
    lRes = DrawMenuBar(Form1.hwnd)
End Sub

Public Function SubClassedForm(ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    Static lLastItemSelected As Long
    If Msg = &H105A Then
        'Redraw Menu
        Call SetMenu(Form1.hwnd, lMnu)
        Call DrawMenuBar(Form1.hwnd)
    ElseIf Msg = WM_MENUSELECT And lParam Then
        'Process Messages From Any of the Forms Menus, Except the System Menu
        If lParam Then
            'A Valid Item was Selected
            'Store the Index in the Static Var Until the Item is Clicked
            lLastItemSelected = wParam And 255
            'Call the MenuEvent Sub with the Last Selected Menu Item ID
            Call MenuEvent(lLastItemSelected)
            lLastItemSelected = 0
        End If
    End If
    SubClassedForm = CallWindowProc(lPrevWnd, hwnd, Msg, wParam, lParam)
End Function

Public Sub MenuEvent(ByVal Index As Long)
    'Menu Item Code Goes Here,
    'Each Menu Item is Identified by it's Unique Index
    With Form1
        Select Case Index
            Case 1
                .Caption = "Open"
                'Open Item Code
            Case 2
                .Caption = "Save"
                'Save Item Code
            Case 3
                .Caption = "Save As.."
                'Save As Item Code
            Case 4
                .Caption = "Close"
                'Exit Item Code
                Unload Form1
            Case Else
                .Caption = "No Item Selected"
        End Select
    End With
End Sub

Option Explicit
Private Sub Form_Load()
    'Get the System Menu Handle, so we don't process it's Messages
    lSysMnu = GetSystemMenu(hwnd, 0)
    'Sub-Class the Form to Capture the Menu Messages
    lPrevWnd = SetWindowLong(hwnd, GWL_WNDPROC, AddressOf SubClassedForm)
    'Create the Menu Completely at Runtime.
End Sub

Private Sub Form_Unload(Cancel As Integer)
    'Remove the Form Sub-Classing *** DO NOT REMOVE ***
    Call SetWindowLong(hwnd, GWL_WNDPROC, lPrevWnd)
End Sub

Author Comment

ID: 2559776
supper man thanks a lot...

Expert Comment

ID: 13689307
I saw your code during my EE search engine for this topic. I tried to implement your codes as suggested but got numerous errors while trying it. I know it's about 5 years you did this - I am sure you probably have a better way of handling the MENU creation in Access. I'll definitely appreciate it if you could take a look at my new post (see the link below) on the subject matter.

My post was done very recently and the question is very much similar to the one you handled here on February 25, 2000 I will appreciate it if you could drop me a line by clicking on my userid which will take you to my profile. Below is the link to my new post.


Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

783 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question