MS-Access / Getting Started

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 next.

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"
[Previous] [Contents] [Next]