DoCmd Object

Description

You can use the methods of the DoCmd object to run Microsoft Access actions from Visual Basic. An action performs tasks such as closing windows, opening forms, and setting the value of controls. For example, you can use the OpenForm method of the DoCmd object to open a form, or use the Hourglass method to change the mouse pointer to an hourglass icon.

Syntax

[application.]DoCmd.method [arg1, arg2, ...]

The DoCmd object uses the following arguments.

Argument Description
application Optional. The Application object.
method One of the methods supported by this object.
arg1, arg2, ... The arguments for the selected method. These arguments are the same as the action arguments for the corresponding action.


Remarks

Most of the methods of the DoCmd object have arguments — some are required, while others are optional. If you omit optional arguments, the arguments assume the default values for the particular action. For example, the OpenForm method uses seven arguments, but only the first argument, formname, is required. The following example shows how you can open the Employees form in the current database. Only employees with the title Sales Representative are included.


DoCmd.OpenForm "Employees", , ,"[Title] = 'Sales Representative'"

The DoCmd object does not support methods corresponding to the following actions:

  • AddMenu
  • MsgBox — Use the MsgBox function.
  • RunApp — Use the Shell function to run another application.
  • RunCode — Run the function directly in Visual Basic.
  • SendKeys — Use the SendKeys statement.
  • SetValue — Set the value directly in Visual Basic.
  • StopAllMacros
  • StopMacro

Methods

ApplyFilter Method, Beep Method, CancelEvent Method, Close Method, CopyObject Method, DeleteObject Method, DoMenuItem Method, Echo Method (DoCmd Object), FindNext Method, FindRecord Method, GoToControl Method, GoToPage Method (DoCmd Object), GoToRecord Method, Hourglass Method, Maximize Method, Minimize Method, MoveSize Method, OpenForm Method, OpenModule Method, OpenQuery Method, OpenReport Method, OpenTable Method, OutputTo Method, PrintOut Method, Quit Method (DoCmd Object), Rename Method, RepaintObject Method, Requery Method (DoCmd Object), Restore Method, RunMacro Method, RunSQL Method, Save Method, SelectObject Method, SendObject Method, SetMenuItem Method, SetWarnings Method, ShowAllRecords Method, ShowToolbar Method, TransferDatabase Method, TransferSpreadsheet Method, TransferText Method.

See Also

ApplyFilter Action, Beep Action, CancelEvent Action, Close Action, CopyObject Action, DeleteObject Action, DoMenuItem Action, Echo Action, FindRecord Action, GoToControl Action, GoToPage Action, GoToRecord Action, Hourglass Action, Maximize Action, Minimize Action, MoveSize Action, OpenForm Action, OpenModule Action, OpenQuery Action, OpenReport Action, OpenTable Action, OutputTo Action, PrintOut Action, Quit Action, Rename Action, RepaintObject Action, Requery Action, Restore Action, RunMacro Action, RunSQL Action, Save Action, SelectObject Action, SendObject Action, SetWarnings Action, ShowAllRecords Action, ShowToolbar Action, TransferDatabase Action, TransferSpreadsheet Action, TransferText Action.

Example

The following example opens a form in Form view and moves to a new record.


Sub NewRecord()
    DoCmd.OpenForm "Employees", acNormal    ' Open form.
    DoCmd.GoToRecord , , acNewRec    ' Move to new record.Sub