Access VBA Macro Programming

(Joao Candeias) #1

does not have properties and methods (commands) that apply to theTableDefobject or the
CurrentDbobject. You cannot use aFieldobject and then issue a command to create a field
to theTableDefobject. This will create an error message because theFieldsits within the
TableDefobject, not the other way around.


Fields("Company").TableDefs("Employees").CreateField


Just as a major cannot give a general an order to advance and attack, this will not work
because theTableDefsobject is at a higher level than theFieldsobject in the hierarchy. It
breaks all the rules of the hierarchy.
There is one way around this: if the general happens to be the major’s father! The general
would listen to his son and then give the order, even though the suggestion comes from lower
down the tree. VBA can work the same way by using theParentproperty. This gives access
to the methods of theParentobject.
Open the Northwind sample database. To do this, open Access and then click Sample in
the left-hand Navigation pane and click the Northwind icon that appears.
Open the form “Order Subform for Order Details” in Design View. You will find this in
the Forms section of the Navigation pane. In the module for this form, add the following code
to the Form Current event:


MsgBox Me.Parent.Name


Save the form and close it. Open the form Order Details and a message box will appear
giving the name of the parent form, which is Order Details. When you open the parent form,
you also open your subform, which then fires the Form Current event. Your code picks up the
name property from the parent object and displays it.
The highest object in the Access object hierarchy is calledApplication(this represents
Access itself). The Access object deals with forms, reports, the current database, theDoCmd
object, and various functions.
The DAO (Data Access Object), as its name implies, deals with accessing and manipulating
the data in the database, creating tables and queries and record sets.
If you are using external databases and tables, you can also use the ADO (Active Data
Objects) model. This is explained further in Chapter 19 on working with external databases.
The structure of the object model is discussed in more detail in Chapter 15. To know the
structure of the object model well, you need to examine the Object Browser (pressF2on the
code sheet to access it) and experiment on a module with the various objects.


Chapter 14: The Access Object Models 191

Free download pdf