Dynamic menus. Create menus at runtime...

I am trying to create dynamic menus at runtime i.e. I want to generate menu items at runtime  by  picking the menu items from a database, but I am unable to find the options to implement that. I have tried using menuitems and giving the menuitems an index value, thus creating a control array for it - but with this the problem is that I cannot create submenuitems for the dropdown menus I am creating. Also I am trying to generate the top level menu items dynamically which with this approach is giving a problem. I have also tried using commandbar control but that always gives wrong number of arguments error or runtime error 5. I would be glad if anyone of you experts could help me out with this problem or could suggest an alternate solution.
Who is Participating?
jgvConnect With a Mentor Commented:
This sample should provide what you are looking for.

TimCotteeHead of Software ServicesCommented:
If you use the standard menu editor to create a control array of menu items with one item having index 0 in each menu could be shown then you can use the load command to instantiate subsequent menu items.
joyriderankAuthor Commented:

Thanks for the prompt response. I have tried that but I have to create a multi level menu and all the heads have to come dynamically. This much I have been able to acomplish but I need to get more functionality added to the menu on runtime.
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

VbAccelerator has many excellent menu solutions.

TimCotteeHead of Software ServicesCommented:
Thinking of alternatives, I use a TreeView control to populate a menu hierarchy from a database. It provides an excellent interface and is extremely flexible.
This example will show you how to dynamically add some menu items to the System menu of a form.  Simply pass the hwnd of the form to the SubClass function.  Keep in mind that it's one thing to add the menu items, but then it's another to be able to respond when the user clicks those menu items.  So, it's necessary to "hook" the "window proc" of the window with the menu.  This will force all window commands through your custom window proc (in the example below that is the SysMenuHandler routine) which will check to see if the message was generated by clicking on one of your dynamic menus, and if so, it will execute some code.  In all cases, it then sends the message along to the original window proc.

There are also APIs for appending sub-menus, and for getting handles to other menus besides the SystemMenu, but I'm not that familiar with that.  But, this should get you looking in the right direction.

Option Explicit
Declare Function AppendMenu Lib "user32" Alias "AppendMenuA" (ByVal hMenu As Long, ByVal wFlags As Long, ByVal wIDNewItem As Long, ByVal lpNewItem As String) As Long
Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Long, ByVal bRevert As Long) As Long
Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
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
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Declare Function IsWindow Lib "user32" (ByVal hwnd As Long) As Long

Public Const WM_SYSCOMMAND = &H112
Public Const MF_SEPARATOR = &H800&
Public Const MF_STRING = &H0&
Public Const GWL_WNDPROC = (-4)
Public Const IDM_CUSTOM As Long = 1010
Public Const IDM_CUSTOM2 As Long = 1011
Public lProcOld As Long
Public ghWnd As Long

Public Function SysMenuHandler(ByVal hwnd As Long, ByVal iMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    If iMsg = WM_SYSCOMMAND Then
        If wParam = IDM_CUSTOM Then
            MsgBox "About...", vbInformation, "About"
            Exit Function
        End If
        If wParam = IDM_CUSTOM2 Then
            MsgBox "Unhooking...", vbInformation, "Unhook"
            SetWindowLong ghWnd, GWL_WNDPROC, lProcOld
            Exit Function
        End If
    End If
    SysMenuHandler = CallWindowProc(lProcOld, hwnd, iMsg, wParam, lParam)
End Function

Public Function SubClass(hwnd As Long)
    Dim lhSysMenu As Long, lRet As Long
    lhSysMenu = GetSystemMenu(hwnd, 0&)
    'Add seperator
    lRet = AppendMenu(lhSysMenu, MF_SEPARATOR, 0&, vbNullString)
    'Add new menu item
    lRet = AppendMenu(lhSysMenu, MF_STRING, IDM_CUSTOM, "About...")
    lRet = AppendMenu(lhSysMenu, MF_STRING, IDM_CUSTOM2, "Unhook")
    lProcOld = SetWindowLong(hwnd, GWL_WNDPROC, AddressOf SysMenuHandler)
End Function
This sample should provide what you are looking for.

joyriderankAuthor Commented:
Thanks for all your comments and suggestion. I have been kept a little buy lately. I will go through your suggestions in the weekend and get back to you guys on Monday. Thanks for your help guys. TAKE CARE.

joyriderankAuthor Commented:
Hi Experts,

Sorry for the delay in getting back to you. First I would like to thank for your suggestions.

MDOUGAN thanks for the code but I am sorry to say I could not make out much from it.

IGV your code was really helpful as it got me to getting the menus created via database connectivity through modifications of course, but I am not able to trap the events for each click. What I mean to say is that now that i have the structure of the menus ready, but what good are they if I can't trap the events of the menus and call the respective form.... So I guess I will raise that question in a new question and am going to post another query regarding the same pretty soon.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.