Renaming a Menu Item

You can use the Caption property of a menu item to change the item's name in response to changing conditions in your Visual Basic code. Suppose, for example, that you've a menu command that opens a database. After the user has opened a database, you may want to replace the original command with a command that closes the database. The following example shows how you could accomplish this.


MenuBars("MyMenubar").Menus("File").MenuItems("Open Database") _
    .Caption = "Close &Database"

When you rename a menu item this way, make sure that the other procedures in your application reference the menu item by its new name (Close Database, in this example).

You can also use variables to refer to a menu item. An advantage of this technique is that variables continue to work even if the item's caption changes. For example, the following line of code sets a variable to the Open Database menu item.


Set myMenu = MenuBars("My Menubar").Menus _
    ("File").MenuItems("Open Database")

You can change the caption later using the following code.


myMenu.Caption = "Close &Database"