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.

Note   The DoCmd object replaces the DoCmd statement from versions 1.x and 2.0 of Microsoft Access. The actions that were used as arguments for the DoCmd statement are now methods of the DoCmd object. For example, in Microsoft Access 2.0, you could have used the code DoCmd OpenForm "Orders" to open a form from Access Basic. In Microsoft Access 97, you would use the following syntax:

DoCmd.OpenForm "Orders"

Syntax

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

The DoCmd object has 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 method. 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 doesn't 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.
For more information on the Microsoft Access action corresponding to a DoCmd method, see the action topic.

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, RunCommand 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, FindNext 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, RunCommand action, RunMacro action, RunSQL action, Save action, SelectObject action, SendObject action, SetMenuItem 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 ShowNewRecord()
    DoCmd.OpenForm "Employees", acNormal
    DoCmd.GoToRecord , , acNewRec
End Sub