RunCommand rather than DoCmd.Object

The following information is taken from an article called "Macros: Tips and Tricks" by Robert Flake of Microsoft Corporation. The original link no longer works so I have removed it.

Should I Use the RunCommand Method or the DoCmd Object Methods?

The methods of the DoCmd object are used in Visual Basic to carry out Microsoft Access macro actions. For example, to carry out the OpenForm action in a Visual Basic procedure, you use the following syntax

  DoCmd.OpenForm arguments

where arguments are the action argument settings you want to use for the OpenForm action.

The RunCommand method is used to carry out a Microsoft Access menu or toolbar command. The RunCommand method replaces the DoMenuItem method used in previous versions of Microsoft Access. To run a particular menu or toolbar command in Visual Basic, use the following syntax

  [Application].RunCommand command
  [DoCmd].RunCommand command

where command is the intrinsic constant corresponding to the menu or toolbar command. The Application or DoCmd prefix is optional. To see a list of the RunCommand method intrinsic constants, search the Microsoft Access Help index for "RunCommand method" and select the "RunCommand Method Constants" topic.

Although in some cases a macro action and a menu command do similar things (for example, the Find action and the Find command on the Edit menu), the methods of the DoCmd object and the RunCommand method are distinct and should be used differently. (The RunCommand method is a method of the DoCmd object, but it has a special purpose: to carry out Microsoft Access menu commands.) The most important distinction is that you when you use the DoCmd methods, you can specify the action argument settings. When you use the RunCommand method, it simply carries out the specified command. If the command brings up a dialog box, the dialog box appears. Using the DoCmd methods allows you to carry out actions in Microsoft Access without having to use menu commands and their dialog boxes.

For example, you may want to have a function procedure that selects the current record in a form and prints it.

Function PrintRecord()
  'This procedure selects the current record and prints it.
  RunCommand acCmdSelectRecord
  DoCmd.PrintOut acSelection
End Function

This function uses the RunCommand method and the intrinsic constant acCmdSelectRecord to carry out the Select Record command on the Edit menu, which selects the current record. Then the PrintOut method of the DoCmd object, with the printrange argument set to acSelection, prints the selected record. Note that if instead of using the PrintOut method of the DoCmd argument, you use the RunCommand method to carry out the Print command on the File menu, the function selects the current record, but brings up the Print dialog box:

Function PrintRecord()
  'This procedure selects the current record but doesn't
  'automatically print it.
  RunCommand acCmdSelectRecord
  RunCommand acCmdPrint
End Function

If you use the SendKeys statement to send an ENTER key to choose OK in the dialog box, the entire set of records is printed, not just the selected record. You could use a series of SendKeys statements to move to the appropriate field in the dialog box and select the Selected Record(s) option, but the PrintOut method of the DoCmd object accomplishes this much more efficiently.

© 1998 - 2011 Terry Wickenden TKW Design Site developed, maintained and hosted by TKW Design. This site is best viewed at 1024 x 768. Optimised for Firefox.