2

I'm working in a VB6 project and there is an existing menu, created with the menu creator. I am having trouble to insert subMenu programmatically in a MenuItem.

The first menu is File. It contains two menu items : Choice and Exit.

I would like to insert each row of a query (only the first column) at run time in Choice.

My recordset works well, but i need some help in the code below :

Do While rs_choice.EOF = False
    'add column1 in Choice
    '~Something~ = rs_choice.Fields("column1").Value
    rs_choice.MoveNext
Loop 

PS : No one MenuItems have defined index.

Can someone help me ?

nbadaud
  • 694
  • 7
  • 26
  • Possible duplicate of [Dynamic two or more levels sub-menu generation in vb6](https://stackoverflow.com/questions/901975/dynamic-two-or-more-levels-sub-menu-generation-in-vb6) – StayOnTarget Mar 05 '19 at 14:43

1 Answers1

4

Using the designer give Choice a single sub-item called mnuDynamic, give it an index of 0.

Loop the recordset loading new items:

Dim i as long

Do While rs_choice.EOF = False
    If (i > 0) Then Load mnuDynamic(i)

    mnuDynamic(i).Caption = rs_choice.Fields("column1").Value

    rs_choice.MoveNext
    i = (i + 1)
Loop
Alex K.
  • 171,639
  • 30
  • 264
  • 288
  • It seems to work, but i have an error when i try to use the Visible property : `the "visible" property can not be defined for this control`. Is it because it is a dynamic menu ? – nbadaud Mar 15 '16 at 16:23
  • I tried this : `If rs_choice.Fields("column2").Value = 0 Then mnuDynamic(i).Visible = True`. And this give me the previous error – nbadaud Mar 15 '16 at 16:33
  • Works for me, just add `if rs_choice.Fields("column1").Value <> 0)` around the loop and don't load the item at all – Alex K. Mar 15 '16 at 16:41
  • +1. This is described in the VB6 user guide [here](https://msdn.microsoft.com/en-us/library/aa262294(v=vs.60).aspx) under `adding menu controls at run time` – MarkJ Mar 15 '16 at 17:17
  • You right, it's better to not load the subMenuItem instead of load it and set it's visible property to false ;) – nbadaud Mar 16 '16 at 07:21