databasedev.co.uk - database solutions and downloads for microsoft access

Microsoft Access DoCmd Object

Using the DoCmd object

The DoCmd object is one of many objects that is offered by the Access environment. It’s parent is the Application object. One of the common uses of the DoCmd object is with Macros. The DoCmd object is used to run Microsoft actions. One of the first ones that we will be looking at is running Form operations with the object.

The following methods (numerous others) can be used with the DoCmd object:

  • Restore – This method takes no parameters and is simply called like this: DoCmd.Restore. It restores a window to its maximized state
  • Minimize – This method takes no parameters and is simply called like this: DoCmd.Minimize
  • GotoRecord – This method takes four arguments, the objectname, record, AcRecord. Below are the explanations for the arguments:

    ObjectName  Optional Variant. A string expression that's the valid name of an object of the type selected by the objecttype argument.

    Record Optional AcRecord.

    AcRecord can be one of these AcRecord constants:
    acFirst
    acGoTo
    acLast
    acNewRec
    acNext default
    acPrevious

  • Hourglass – Takes one argument, which is the hourglassOn. This argument is required and has the following format:

    True or -1 to display the hourglass icon or a custom icon of your choice
    False or 0 (zero) – To display normal mouse pointer.
  • Openform – This method does exactly what the name says, it opens up a form and takes seven arguments of which only the first one – the formname - is required.
  • Close – This method closes a form. It does not take any arguments at all.
  • Createform – Take one argument. Its purpose is to create a form.

This is but a few of the many methods that is available to, and that can be used with the DoCmd object. So let’s take a look at some of the above methods.

The openform method does not only enable us to open a form, it can also open a form and point the database to show a particular record. Say we want to open a form and display all records with the surname ‘Nemo’ in our contact database. Then this is how we will do it with the openform method:

DoCmd.OpenForm "Contacts", , , "Surname = 'Nemo'"

In the above screenshot, we have two forms, one that has a button that when clicked opens the second form at the specified record. If the second form is maximized and you want it to be restored then you simply add the following line of code:

DoCmd.Restore after calling the openform method.

If we wanted to go to say record number two in our contacts database, we would write:

DoCmd.GoToRecord acDataForm, "contacts", acGoTo, 2

So the DoCmd object makes it very easy to manipulate both records and Form operations

The Author

Leidago !Noabeb is a computer programmer based in Namibia. He has worked with both opensource and Microsoft technologies for over seven years and specializes in writing communications software. He has made many contributions to various online websites dedicated to web development. He can be reached at: leidago [at] googlemail.com