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 ActionsMacro 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.
In this tutorial:
- Visual Basic Fundamentals
- Visual Basic Development Environment
- Visual Basic Editor Window
- Relationship Between Access and Visual Basic
- Visual Basic Debugging Tools
- Working with the Watch Window
- Variables and Constants
- Variable and Constant Scope
- Declaring Constants and Variables
- Dim Statement
- Enum Statement
- Event Statement
- Private Statement
- Public Statement
- Static Statement
- Type Statement
- Collections, Objects, Properties, and Methods
- DAO Architecture
- ADO Architecture
- Referencing Collections, Objects, and Properties
- Use Exclamation Points and Periods
- Assigning an Object Variable-Set Statement
- Object Methods
- Manipulating Complex Data Types Using DAO
- Working with ADO Recordsets
- Functions and Subroutines
- Sub Statement
- Understanding Class Modules
- Property Let
- Property Set
- Controlling the Flow of Statements
- Do...Loop Statement
- For...Next Statement
- For Each...Next Statement
- If...Then...Else Statement
- RaiseEvent Statement
- Stop Statement
- With...End Statement
- Running Macro Actions and Menu Commands
- Executing an Access Command
- Trapping Errors
- Working with 64-Bit Access Visual Basic for Applications
- Using LongPtr Data Types
- Supporting Older Versions of Access
- Using LongLong Data Types