MS-Access / Getting Started

Executing an Access Command

To execute an Access command (one of the commands you can find on the ribbon), use the RunCommand method of either the DoCmd or Application object and supply a single action argument that is the numeric code for the command.

Syntax

[DoCmd.]RunCommand [actionargument],...

Notes

You can also use one of many built-in constants for actionargument to reference the command you want. When you use RunCommand, you can leave out the DoCmd or Application object if you want.

Examples

To execute the Save command from the Records group on the Home tab, enter the following:

RunCommand acCmdSaveRecord

To switch an open form to PivotChart view (execute the PivotChart View command in the Views group on the Home tab), enter the following:

RunCommand acCmdPivotChartView

To open the Find window while the focus is on a form (execute the Find command in the Find group on the Home tab), enter the following:

RunCommand acCmdFind
Note: Visual Basic provides built-in constants for many of the macro action and RunCommand parameters. For more information, search for "Microsoft Access Constants" and "RunCommand Method" in Help.

Actions with Visual Basic Equivalents

A few macro actions cannot be executed from a Visual Basic procedure. All but one of these actions, however, have equivalent statements in Visual Basic, as shown in Table-4.

Table-4 Visual Basic Equivalents for Macro Actions
Macro Action 		Visual Basic Equivalent
AddMenu 		No equivalent
MessageBox 		MsgBox statement or function
RunApplication* 	Shell function
RunCode 		Call subroutine
SendKeys 		SendKeys statement
SetValue 		Variable assignment (=)
StopAllMacros 		Stop or End statement
StopMacro 		Exit Sub or Exit Function statement
* Database must be Trusted to execute this action.
[Previous] [Contents] [Next]