Add a function to a button or command

Microsoft Office Access 2003

  1. Create the function you want the button to run.

    ShowHow?

    1. To open a module, do one of the following:

      To open a new standard module, in the Database window, click Modules Button image under Objects, and then click the New button on the Database window toolbar.

      To open an existing standard module, click Modules Button image 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 Button image 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 Button image under Objects, select the module you want to open, and then click the Design button on the Database window toolbar.

    2. Declare the function by typing the Function statement.
    3. 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
      										
    4. Add the Microsoft Visual Basic code that performs the operation or calculation that you want the function to perform.
  2. On the View menu, point to Toolbars, and then click Customize.
  3. 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.
  4. In the Customize dialog box, click the Commands tab.
  5. In the Categories box, click File.
  6. Drag the Custom command from the Commands box to the desired toolbar within the program window.
  7. 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.
  8. On the same shortcut menu, click Properties.
  9. 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")).
  10. Specify any other properties for the button.

ShowAdd a custom command that runs a Visual Basic function to a menu

  1. Create the function you want the command to run.

    ShowHow?

    1. To open a module, do one of the following:

      To open a new standard module, in the Database window, click Modules Button image under Objects, and then click the New button on the Database window toolbar.

      To open an existing standard module, click Modules Button image 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 Button image 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 Button image under Objects, select the module you want to open, and then click the Design button on the Database window toolbar.

    2. Declare the function by typing the Function statement.
    3. 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
      										
    4. Add the Microsoft Visual Basic code that performs the operation or calculation that you want the function to perform.
  2. On the View menu, point to Toolbars, and then click Customize.
  3. 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.
  4. In the Customize dialog box, click the Commands tab.
  5. In the Categories box, click File.
  6. 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.
  7. 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.
  8. On the same shortcut menu, click Properties.
  9. 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")).
  10. 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.