Working with the DoCmd Object
The DoCmd object makes it possible for you to carry out various Microsoft® Access commands by using Microsoft® Visual Basic® for Applications (VBA). These commands are called actions when they are used in Access macros and are called methods of the DoCmd object when they are carried out in code.
Note In other Microsoft® Office applications, the term "macro" is synonymous with a VBA procedure. In Access, macros are completely different from the VBA code you write in a procedure. For more information about Access macros, search the Microsoft® Access Help index for "macros, overview," and then open the topic "Macros: What they are and how they work."
Two of the most common tasks that require methods of the DoCmd object are opening and closing Access objects. To open an Access object, you use the DoCmd object's OpenObject method, where Object represents the name of the object you want to open. For example, you use the OpenForm method to open a form, the OpenReport method to open a report, and the OpenQuery method to open a query. All of the OpenObject methods take arguments that specify the object to open and how to display the object. For example, the following code opens the Customers form as read-only in Form view (acNormal) and specifies that only customers in the USA be shown:
DoCmd.OpenForm FormName:="Customers", View:=acNormal, _
WhereCondition:="Country = 'USA'", DataMode:=acFormReadOnly
You can use the OpenReport method to open a report in Design view or Print Preview, or you can specify that the report be printed, as in the following example:
DoCmd.OpenReport ReportName:="CustomerPhoneList", _
View:=acViewNormal, WhereCondition:="Country = 'USA'"
Note When you use the acViewNormal constant in the view argument of the OpenReport method, the report is not displayed but is printed to the default printer.
You use the DoCmd object's Close method to close an Access object. You can use the optional arguments of the Close method to specify the object to close and whether to save any changes. The following example closes the Customers form without saving changes:
DoCmd.Close acForm, "Customers", acSaveNo
Note All the arguments of the Close method are optional. If you use the method without specifying arguments, the method closes the currently active object.
You can use the DoCmd object's RunCommand method to run commands that appear on an Access menu or toolbar that do not have separate methods exposed in the Access object model. The RunCommand method uses a collection of enumerated constants to represent available menu and toolbar commands. For more information about the RunCommand method, search the Microsoft® Access Visual Basic® Reference Help index for "RunCommand method."
See Also
Built-in Access Functions and Methods | Creating, Opening, and Closing an Access Application | The CurrentData and CurrentProject Objects | Working with the Screen Object | Working with the Modules Collection | Working with the References Collection