mnuItems at runtime

Posted on 1998-12-14
Last Modified: 2010-05-03
Is there a way to add menu items to a menu at runtime?
Question by:gwu

Accepted Solution

polygon earned 50 total points
ID: 1450057
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.

Author Comment

ID: 1450058
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.

Expert Comment

ID: 1450059
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.


Expert Comment

ID: 1450060
     mnuFileOpenNetwork.Enabled = True
     mnuFileSaveNetwork.Enabled = True
those are something specific to my project.

Expert Comment

ID: 1450061
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

Expert Comment

ID: 1450062
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


Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
vb6 programatically bring window to front 8 75
using Access 8 75
VBA/SQL - Connect to SQL server and pull data 4 118
Add a task in Outlook from access 11 42
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

856 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