Running Macro Actions and Menu Commands


From within Visual Basic, you can execute most of the macro actions that Access provides and any of the built-in menu commands. Only a few of the macro actions have direct Visual Basic equivalents. To execute a macro action or menu command, use the methods of the DoCmd object, described below.

DoCmd Object

Use the methods of the DoCmd object to execute a macro action or menu command from within a Visual Basic procedure.

Syntax

 DoCmd.actionmethod [actionargument],...

Notes

Some of the macro actions you’ll commonly execute from Visual Basic include ApplyFilter, Close, FindNext and FindRecord (for searching the recordset of the current form and immediately displaying the result), Hourglass, Maximize, Minimize, MoveSize, OpenForm, OpenQuery (to run a query that you don’t need to modify), OpenReport, and RunCommand. Although you can run the Echo, GoToControl, GoToPage, RepaintObject, and Requery actions from Visual Basic using a method of the DoCmd object, it’s more efficient to use the Echo, SetFocus, GoToPage, Repaint, and Requery methods of the object to which the method applies.

Examples

To open a form named frmCompanies in Form view for data entry, enter the following:

 DoCmd.OpenForm "frmCompanies", acNormal, , , acAdd

To close a form named frmContacts, enter the following:

 DoCmd.Close acForm, "frmContacts"

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 optionally leave out the DoCmd or Application object.

Examples

To execute the Save command in 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 on “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 19–4.

Table 19–4: Visual Basic Equivalents for Macro Actions
Open table as spreadsheet

Macro Action

Visual Basic Equivalent

AddMenu

No equivalent

MsgBox

MsgBox statement or function

RemoveAllTempVars

TempVars.RemoveAll

RemoveTempVar

TempVars.Remove variablename

RunApp[1]

Shell function

RunCode

Call subroutine

SendKeys

SendKeys statement

SetTempVar

TempVars!variablename=value

SetValue

Variable assignment (=)

StopAllMacros

Stop or End statement

StopMacro

Exit Sub or Exit Function statement

[1] Database must be trusted to execute this action.




Microsoft Office Access 2007 Inside Out
MicrosoftВ® Office Access(TM) 2007 Inside Out (Microsoft Office Access Inside Out)
ISBN: 0735623252
EAN: 2147483647
Year: 2007
Pages: 234

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net