- Create the function you want the button to run.
- To open a module, do one of the following:
To open a new standard module, in the Database window, click Modules under Objects, and then click the New button on the Database window toolbar.
To open an existing standard module, click Modules under Objects, select the module you want to open, and then click Design.
To open a form module or report module, open the form or report in Design view, and then click Code on the toolbar.
To open a new class module that isn't associated with a form or report, in the Database window click Class Module on the Insert menu.
To open an existing class module, in the Database window, click Modules under Objects, select the module you want to open, and then click the Design button on the Database window toolbar.
- Declare the function by typing the Function statement.
- Type a function name immediately followed by any function arguments in parentheses. For example, the following declaration for the IsLoaded function specifies strFormName as an argument:
Function IsLoaded (strFormName As String) As Boolean
- Add the Microsoft Visual Basic code that performs the operation or calculation that you want the function to perform.
- To open a module, do one of the following:
- On the View menu, point to Toolbars, and then click Customize.
- If the toolbar isn't already shown within the program window, click the Toolbars tab, and then double-click the toolbar name you want to show.
- In the Customize dialog box, click the Commands tab.
- In the Categories box, click File.
- Drag the Custom command from the Commands box to the desired toolbar within the program window.
- With the Customize dialog box open, right-click the button on the toolbar. On the shortcut menu, type a new name for the button in the Name box.
- On the same shortcut menu, click Properties.
- In the On Action box, type the name of the function you want to run in the format =functionname(). For example, for a custom function named SetCaption(), type =SetCaption(). For a built-in function, enter the name of the function and any required arguments; for example =MsgBox(IIf(Instr(Time(), "PM"), "Good Afternoon", "Good Morning")).
- Specify any other properties for the button.
Add a custom command that runs a Visual Basic function to a menu
- Create the function you want the command to run.
- To open a module, do one of the following:
To open a new standard module, in the Database window, click Modules under Objects, and then click the New button on the Database window toolbar.
To open an existing standard module, click Modules under Objects, select the module you want to open, and then click Design.
To open a form module or report module, open the form or report in Design view, and then click Code on the toolbar.
To open a new class module that isn't associated with a form or report, in the Database window click Class Module on the Insert menu.
To open an existing class module, in the Database window, click Modules under Objects, select the module you want to open, and then click the Design button on the Database window toolbar.
- Declare the function by typing the Function statement.
- Type a function name immediately followed by any function arguments in parentheses. For example, the following declaration for the IsLoaded function specifies strFormName as an argument:
Function IsLoaded (strFormName As String) As Boolean
- Add the Microsoft Visual Basic code that performs the operation or calculation that you want the function to perform.
- To open a module, do one of the following:
- On the View menu, point to Toolbars, and then click Customize.
- If the toolbar, menu bar, or shortcut menu isn't already shown within the program window, click the Toolbars tab, and then double-click the toolbar, menu bar, or shortcut menu name you want to show.
- In the Customize dialog box, click the Commands tab.
- In the Categories box, click File.
- Drag the Custom command from the Commands box over the menu on the menu bar or the toolbar, or under the appropriate category on the Shortcut Menus toolbar. When the menu displays a list of menu commands (or an empty box if it's new), point to the location where you want the command to appear on the menu, and then release the mouse button.
- With the Customize dialog box open, right-click the command on the menu. On the shortcut menu, type a new name for the command in the Name box.
- On the same shortcut menu, click Properties.
- In the On Action box, type the name of the function you want to run in the format =functionname(). For example, for a custom function named SetCaption(), type =SetCaption(). For a built-in function, enter the name of the function and any required arguments; for example =MsgBox(IIf(Instr(Time(), "PM"), "Good Afternoon", "Good Morning")).
- Specify any other properties for the command.
Note If you add a command to a built-in menu (for example, the Edit menu), that command will appear in all views containing that built-in menu.