Articles and Tutorials

What You Need to Know About CommandBar Control IDs

Author: Mike Palmatier
Platform: Visual Basic 6.0

Each CommandBar control needs to have a unique ID so that you will know when a button or menu item has been pressed. There are many other things the ID is used for such as updating the control, searching for the control, and associating an icon with the control. The ID of the control is assigned when the control is added to the menu or toolbar.

If a toolbar item should perform the same function as a menu item, then the ID for both the menu item and toolbar button should be the same.

Valid IDs that can be used for CommandBar controls range from 100-9000. There are also some IDs which have a special meaning that is discussed in the Special Control IDs.

It is recommended that you declare a Constant value for your CommandBar IDs that that they can easily be identified throughout the code. We recommended that you follow some type of naming scheme. For example, for a menu item located in "File->New" you, would use an ID this: "ID_NEW_FILE" starting with the prefix ID, followed by the name of the menu item - all separated by underscores.

You can place the Constant ID values wither in a separate .bas file or in the "General" code section of the form the CommandBars control is located. You declare a Const when included as part of an object module, and a Public Const when in a .bas file.

Below is some sample CommandBar Control IDs that are declared in the "General" code section of a form.

Const ID_FILE_NEW   = 100
Const ID_FILE_OPEN  = 101
Const ID_FILE_CLOSE = 102
Const ID_FILE_SAVE  = 103
Const ID_FILE_EXIT  = 104

Articles and Tutorials