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.
Use the methods of the DoCmd object to execute a macro action or menu command from within a Visual Basic procedure.
DoCmd.actionmethod [actionargument],...
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.
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"
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.
[DoCmd.] RunCommand [actionargument], ...
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.
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. |
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.
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. |