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:
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:
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