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

Dynamic Menus without using Manu Editor

Status
Not open for further replies.

vbprogramming

Programmer
Jun 3, 2003
7
0
0
US
I’m stuck, I have been working on this for too long without getting anywhere! I have an application where the user starts with a splash screen. The startup window will need menus on a tool bar at the top. When the application starts it will scan an external mdb file for a list of all the menu names to add to the tool bar. I need the menus to be totally dynamic. I have looked for hours on multiple code sites and have been unable to resolve this issue. Following is what I want the code to look like:

Public sub Form_load()
Do other non related tasks……
…..
loadMenus()
End sub

Private sub loadMenus()
Dim dbs as database
Dim rst as recordset

Set dbs = opendatabase(“C:\file etc\example.mdb”)
Set rst = dbs.openrecordset(“select * from tablename where [field] = ‘value’”)
While not rst.eof
‘?????
‘create a new dropdown menu for the toolbar with the name as defined
‘by rst.field(“Menu Name”)
‘?????
wend
End sub

The purpose for this is that I have created a common interface into many different sub programs. I want to just pass to each user the modules that they need. The user will push a button and the mdb file of menu names will be used to repopulate the menus. The end result is that I will just add a new recordset into the database and be able to with my code automatically add in a new menu.
 
Yes, you can do this, but it's rather limited. There's a way to cheat, though.

First, you can use the load command if that will serve your purposes. You have to have a control array, and load as follows:

Load MyExistingControlName(UnusedIndexNumber)

So, you have to have an existing menu item that's a sibling of the one you want to load. Of course, you can play with visibility and stuff, but what you can't do is add horizontal menu items and then drop down items from there.

Now. If you're a brave soul, you can play with the .frm files directy with text file manipulation commands. This should work fine, but you have to make sure that, say, turning off the machine doesn't screw up the initial form file. Perhaps a sub main that loads the splash screen, then opens a new .frm file, writes the file up to the begin statements for the menu commands, messes around with your recordset and writes the begin statements for the menu files, and then appends the rest of the .frm file, would take care of things.

HTH

Bob Rodes
 
>Yes, you can do this, but it's rather limited. There's a
>way to cheat, though.

>First, you can use the load command if that will serve
>your purposes. You have to have a control array, and load
>as follows:

>Load MyExistingControlName(UnusedIndexNumber)

I haven't been unable to set the control array
ie:
I want a toolbar at the top with no static menu's. I see no place in the code where I can refrence the standard toolbar. Even if I create a new menu in the code I have not been able to attach it to a toolbar. I don't understand how I can load myExistingControlName if I don't know of an existing control. Are you creating a standard menu object and loading it to the form?

>So, you have to have an existing menu item that's a
>sibling of the one you want to load. Of course, you can

I think part of the problem I am having is that I only know how to add a menu through the "dumb" menu editor feature in VB. This does not give you the option of creating an array of menus. I want to know the method that this is using when it creates a menu and how it attaches that menu to a toolbar.

>play with visibility and stuff, but what you can't do is
>add horizontal menu items and then drop down items from
>there.

Does this answer my question stating that it is not possible to dynamically add multiple menus to a toolbar?

>Now. If you're a brave soul, you can play with the .frm
>files directy with text file manipulation commands. This
>should work fine, but you have to make sure that, say,
>turning off the machine doesn't screw up the initial form
>file. Perhaps a sub main that loads the splash screen,
>then opens a new .frm file, writes the file up to the
>begin statements for the menu commands, messes around with
>your recordset and writes the begin statements for the
>menu files, and then appends the rest of the .frm file,
>would take care of things.

I am thinking about this option. If I can't get what I want out of the code I will do it this way.

>HTH

>Bob Rodes

thanks for the input Bob.
 
"If you're a brave soul, you can play with the .frm files directy with text file manipulation commands." This doesn't solve his problem. He needs modification AFTER compilation.

Yes you can load new controls in a control array there must be 1 control already there and at the level you want. Thus if you want to dynamically create both a menu accross and down for each one of them load will not work. me.controls.add also does not allow "vb.menu" to be added. Thus you are left to APIs 8(

Looking for the code but here is the start for you

Code:
Public Declare Function GetMenu Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function SetMenu Lib "user32" (ByVal hwnd As Long, ByVal hMenu As Long) As Long
Public Declare Function GetSubMenu Lib "user32" (ByVal hMenu As Long, ByVal nPos As Long) As Long
Public Declare Function SetMenuItemBitmaps Lib "user32" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long, ByVal hBitmapUnchecked As Long, ByVal hBitmapChecked As Long) As Long
Public Declare Function GetMenuItemID Lib "user32" (ByVal hMenu As Long, ByVal nPos As Long) As Long
Public Declare Function RemoveMenu Lib "user32" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long) As Long
Public 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
Public Declare Function InsertMenu Lib "user32" Alias "InsertMenuA" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long, ByVal wIDNewItem As Long, ByVal lpNewItem As Any) As Long
Public Declare Function DestroyMenu Lib "user32" (ByVal hMenu As Long) As Long
Public Declare Function CreateMenu Lib "user32" () As Long
Public Declare Function CreatePopupMenu Lib "user32" () As Long
Public Declare Function DrawMenuBar Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function LoadImage Lib "user32" Alias "LoadImageA" (ByVal hInst As Long, ByVal lpsz As String, ByVal un1 As Long, ByVal n1 As Long, ByVal n2 As Long, ByVal un2 As Long) As Long

Public Const MF_BYCOMMAND = &H0&
Public Const MF_BYPOSITION = &H400&

Public Const MF_BITMAP = &H4&
Public Const MF_STRING = &H0&
Public Const MF_ENABLED = &H0&
Public Const MF_POPUP = &H10&
Public Const MF_SEPARATOR = &H800&

Public Const LR_LOADMAP3DCOLORS = &H1000
Public Const LR_LOADFROMFILE = &H10
Public Const LR_LOADTRANSPARENT = &H20
Public Const IMAGE_BITMAP = 0
 
>vbprogramming wrote:

>I haven't been unable to set the control array
>ie:

sorry about the double double negatives

>SemperFiDownUnda wrote:

>Looking for the code but here is the start for you

>Public Declare Function GetMenu Lib "user32" (ByVal hwnd
>As Long) As Long
>Public Declare ......

This looks more like what I had in mind.
 
Semperfi is right, that way of doing it won't work after compiling. Oh, well, it was an idea.

All right, here we are. The menu editor *does* allow you to make control arrays. You just set a value for the index. You still have the limitations we mentioned, though. Perhaps this is enough for your needs.

Bob
 
>BobRodes wrote:

>All right, here we are. The menu editor *does* allow you
>to make control arrays. You just set a value for the
>index. You still have the limitations we mentioned,
>though. Perhaps this is enough for your needs.

How does this work? If I declare one menu with and index of 0 or 1 then how do I dynamically add the other menus. Can I redim the menu to the next available value? If I add a second and third menu wont I be working outside of the array boundaries?

What I want in the end result is:
take for example a standard word session. You have a toolbar that has menus File, Edit, View, Insert etc. Each of these menus have their sub menus.

When my program loads it has no menus or at most one menu it scans a mdb file for a list of installed modules. It then creates a list of menus ie instead of File, Edit, View, Insert etc it would have Configure, module1, module2, module3, module4 etc while another user only has two modules so they would get Configure, module1, module2. When you click on module2 you would get a dropdown of available options for that module.
 
The problem with Load is that you can not create submenus under the newly loaded menu item (if I'm wrong I'd love to see the code) It is ok if you have a menu that you need a variable number of items within it but as soon as you say "I want a new menu named blah and it will have these new items under it" your stuck.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top