• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 280
  • Last Modified:

mnuItems at runtime

Is there a way to add menu items to a menu at runtime?
1 Solution
create a menu array. If you know what items will be added, you may simply create them hidden and show them when needed.
If you don't know what will the items be, like when creating Recent Files list, you will need a menu array. It is created by adding same name for several items and different indeces.

Also, if you do know how much your items will be, but don't know what will they contain, you nay again just create them, set them to hidden and change them before you need to shoe 'em.
gwuAuthor Commented:
I can see that creating a menu array could work, but if I want to add new menu Items at runtime, I don't see a way to do that given the methods and properties available.
Here is some code for you, this adds menu items dynamically during load from a database.

On Error GoTo ErrHandler

    Dim strNameFile As String
    Dim strName As String
    Dim dbsNames As Database
    Dim rstNames As Recordset
    Dim intRet As Integer
    Dim i As Integer
    strNameFile = strNetworkDB
    Set dbsNames = OpenDatabase(strNameFile)
    Set rstNames = dbsNames.OpenRecordset("tblNames")
    Name_Idx = 0
    Do Until rstNames.EOF
        If Name_Idx > 0 Then
            Load Me.mnuFileOpenName(Name_Idx)
        End If
        Me.mnuFileOpenName(Name_Idx).Caption =         rstNames!Name
        Name_Idx = Name_Idx + 1
    mnuFileOpenNetwork.Enabled = True
    mnuFileSaveNetwork.Enabled = True
    Exit Sub

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

     mnuFileOpenNetwork.Enabled = True
     mnuFileSaveNetwork.Enabled = True
those are something specific to my project.
I saw this and have done:

Private Sub mnuViewSort_Click()
Dim RS As Recordset
Dim i As Integer

Set RS = DB.OpenRecordset("Routenametable", dbOpenSnapshot)

Me.mnuDynRoutes(0).Visible = True

i = 0

While Not RS.EOF
    i = i + 1
    Load Me.mnuDynRoutes(i)
    Me.mnuDynRoutes(i).Caption = RS!Name

If i > 0 Then Me.mnuDynRoutes(0).Visible = False


End Sub

I'm using the first (0) menu item as a "No items here" tag
Oh yeah, I had to create at least one mnu item (Hence the 0 index) at design time to kick off the array. and I used this to clear them:

For Each m In mnuDynRoutes
    If m.Index <> 0 Then Unload m
Next m

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.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now