The DoCmd Object: Performing Macro Actions
The Access environment is rich with objects that have built-in properties and methods. By using VBA code, you can modify the properties and execute the methods. One of the objects available in Access is the DoCmd object, used to execute macro actions in Visual Basic procedures. The macro actions are executed as methods of the DoCmd object. The syntax looks like this:DoCmd.ActionName [arguments]
Here's a practical example:DoCmd.OpenReport strReportName, acPreview
The OpenReport method is a method of the DoCmd object; it runs a report. The first two parameters that the OpenReport method receives are the name of the report you want to run and the view in which you want the report to appear (Preview, Normal, or Design). The name of the report and the view are both arguments of the OpenReport method.Most macro actions have corresponding DoCmd methods that you can find in Help, but some don't. They are AddMenu, MsgBox, RunApp, RunCode, SendKeys, SetValue, StopAllMacros, and StopMacro. The SendKeys method is the only one that has any significance to you as a VBA programmer. The remaining macro actions either have no application to VBA code, or you can perform them more efficiently by using VBA functions and commands. The VBA language includes a MsgBox function, for example, that's far more robust than its macro action counterpart.Many of the DoCmd methods have optional parameters. If you don't supply an argument, its default value is assumed. You can use commas as place markers to designate the position of missing arguments, as shown here:DoCmd.OpenForm "frmOrders", , ,"[OrderAmount] > 1000"
The OpenForm method of the DoCmd object receives seven parameters; the last six parameters are optional. In the example, two parameters are explicitly specified. The first is the name of the form ("FrmOrders"), a required parameter. The second and third parameters have been omitted, meaning that you're accepting their default values. The commas, used as place markers for the second and third parameters, are necessary because one of the parameters following them is explicitly designated. The fourth parameter is the Where condition for the form, which has been designated as the record in which the OrderAmount is greater than 1,000. The remaining parameters haven't been referred to, so default values are used for these parameters.If you prefer, you can use named parameters to designate the parameters that you are passing. Named parameters, covered later in this chapter, can greatly simplify the preceding syntax. With named parameters, the arguments don't need to be placed in a particular order, nor do you need to worry about counting commas. The preceding syntax can be changed to the following:DoCmd.OpenForm FormName:="frmOrders", WhereCondition:=
"[OrderAmount] > 1000"